home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / netroute / RCS / netroute.c,v < prev    next >
Encoding:
Text File  |  1992-08-18  |  76.7 KB  |  3,103 lines

  1. head     1.21;
  2. branch   ;
  3. access   ;
  4. symbols  sprited:1.15.1;
  5. locks    jhh:1.21; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.21
  10. date     92.06.23.12.08.36;  author jhh;  state Exp;
  11. branches ;
  12. next     1.20;
  13.  
  14. 1.20
  15. date     92.06.23.11.40.18;  author jhh;  state Exp;
  16. branches ;
  17. next     1.19;
  18.  
  19. 1.19
  20. date     92.06.23.11.37.52;  author jhh;  state Exp;
  21. branches ;
  22. next     1.18;
  23.  
  24. 1.18
  25. date     92.06.23.11.31.04;  author jhh;  state Exp;
  26. branches ;
  27. next     1.17;
  28.  
  29. 1.17
  30. date     92.03.25.21.19.40;  author voelker;  state Exp;
  31. branches ;
  32. next     1.16;
  33.  
  34. 1.16
  35. date     92.02.14.19.11.29;  author jhh;  state Exp;
  36. branches ;
  37. next     1.15;
  38.  
  39. 1.15
  40. date     92.02.14.19.10.50;  author jhh;  state Exp;
  41. branches 1.15.1.1;
  42. next     1.14;
  43.  
  44. 1.14
  45. date     90.12.01.21.00.48;  author jhh;  state Exp;
  46. branches ;
  47. next     1.13;
  48.  
  49. 1.13
  50. date     90.09.26.12.12.21;  author jhh;  state Exp;
  51. branches ;
  52. next     1.12;
  53.  
  54. 1.12
  55. date     89.09.14.13.16.43;  author douglis;  state Exp;
  56. branches ;
  57. next     1.11;
  58.  
  59. 1.11
  60. date     89.07.07.14.46.58;  author mendel;  state Exp;
  61. branches ;
  62. next     1.10;
  63.  
  64. 1.10
  65. date     89.06.29.10.26.03;  author mendel;  state Exp;
  66. branches ;
  67. next     1.9;
  68.  
  69. 1.9
  70. date     89.06.08.11.55.25;  author mendel;  state Exp;
  71. branches ;
  72. next     1.8;
  73.  
  74. 1.8
  75. date     89.04.23.16.25.47;  author jhh;  state Exp;
  76. branches ;
  77. next     1.7;
  78.  
  79. 1.7
  80. date     89.04.23.16.21.39;  author brent;  state Exp;
  81. branches ;
  82. next     1.6;
  83.  
  84. 1.6
  85. date     88.12.22.13.12.04;  author brent;  state Exp;
  86. branches ;
  87. next     1.5;
  88.  
  89. 1.5
  90. date     88.11.12.12.06.18;  author brent;  state Exp;
  91. branches ;
  92. next     1.4;
  93.  
  94. 1.4
  95. date     88.03.31.10.06.42;  author brent;  state Exp;
  96. branches ;
  97. next     1.3;
  98.  
  99. 1.3
  100. date     87.10.06.13.01.46;  author deboor;  state Exp;
  101. branches ;
  102. next     1.2;
  103.  
  104. 1.2
  105. date     87.09.30.21.52.46;  author deboor;  state Exp;
  106. branches ;
  107. next     1.1;
  108.  
  109. 1.1
  110. date     87.08.05.22.20.53;  author andrew;  state Exp;
  111. branches ;
  112. next     ;
  113.  
  114. 1.15.1.1
  115. date     92.03.26.17.16.54;  author kupfer;  state Exp;
  116. branches ;
  117. next     ;
  118.  
  119.  
  120. desc
  121. @initial check-in.
  122. @
  123.  
  124.  
  125. 1.21
  126. log
  127. @use whatever the default host file is, rather than setting one
  128. @
  129. text
  130. @/* 
  131.  * netroute.c --
  132.  *
  133.  *    User program to install routes to Sprite Hosts.  'route' is a misnomer
  134.  *    because the information also includes the hosts name and its
  135.  *    machine type.  Plus, the route is just a local address. 
  136.  *
  137.  * Copyright 1989 Regents of the University of California
  138.  * Permission to use, copy, modify, and distribute this
  139.  * software and its documentation for any purpose and without
  140.  * fee is hereby granted, provided that the above copyright
  141.  * notice appear in all copies.  The University of California
  142.  * makes no representations about the suitability of this
  143.  * software for any purpose.  It is provided "as is" without
  144.  * express or implied warranty.
  145.  */
  146.  
  147. #ifndef lint
  148. static char rcsid[] = "$Header: /sprite/src/cmds/netroute/RCS/netroute.c,v 1.20 92/06/23 11:40:18 jhh Exp $ SPRITE (Berkeley)";
  149. #endif not lint
  150.  
  151.  
  152. #include <sprite.h>
  153. #include <stdlib.h>
  154. #include <string.h>
  155. #include <bstring.h>
  156. #include <fs.h>
  157. #include <net.h>
  158. #include <host.h>
  159. #include <kernel/netTypes.h>
  160. #include <stdio.h>
  161. #include <option.h>
  162. #include <rpc.h>
  163. #include <kernel/rpcPacket.h>
  164. #include <sysStats.h>
  165. #include <sys/types.h>
  166. #include <sys/stat.h>
  167. #include <errno.h>
  168. #include <sys/time.h>
  169. #include <gate.h>
  170.  
  171. Net_NetworkType rankedInterfaceTypes[HOST_MAX_INTERFACES] =
  172.     { NET_ADDRESS_ETHER, NET_ADDRESS_FDDI, NET_ADDRESS_ULTRA };
  173.  
  174. static char *rankedInterfaceNames[HOST_MAX_INTERFACES] =
  175.     { "ether", "fddi", "ultra" };
  176.  
  177. static char *netTypeNames[NET_NUM_NETWORK_TYPES] = 
  178.     { "Ethernet", "UltraNet", "FDDI" };
  179.  
  180. #define NUM_NET_ADDRS    5
  181. static char *netAddressNames[NUM_NET_ADDRS] =
  182.     { "No Address", "Ethernet", "UltraNet", "FDDI", "Internet" };
  183.  
  184. static char *netProtocolNames[NET_MAX_PROTOCOLS] = 
  185.     { "raw", "inet" };
  186.  
  187. /*
  188.  * Parameters set by the command line
  189.  */
  190. char   *inputFilename = NULL;
  191. char   *hostName = NULL;
  192. int     first = -1;
  193. int     last = -1;
  194.  
  195. char   *gateFilename;
  196.  
  197. Boolean install = TRUE;
  198. int delete  = -1;
  199. Boolean print   = FALSE;
  200.  
  201. char   *favorName = NULL;
  202. char   *disfavorName = NULL;
  203.  
  204. char   *etherString = NULL;
  205. char   *ultraString = NULL;
  206. char   *fddiString = NULL;
  207.  
  208. Boolean verbose = FALSE;
  209. Boolean execute = FALSE;
  210. char   *machType;
  211. int     hostID = -1;
  212.  
  213. Option optionArray[] = {
  214.     {OPT_DOC, "\0", (Address)NULL, "  Hosts:"},
  215.     {OPT_STRING, "f", (Address)&inputFilename, 
  216.      "Specifies the host database file."},
  217.     {OPT_STRING, "h", (Address)&hostName, 
  218.      "Use Sprite host with given name or Sprite ID"},
  219.     {OPT_STRING, "g", (Address)&gateFilename,
  220.      "Specifies the gateway database file."},
  221.     {OPT_DOC, "\0", (Address)NULL, "  Actions:"},
  222.     {OPT_TRUE, "i", (Address)&install,
  223.      "Install route(s)."},
  224.     {OPT_INT, "d", (Address)&delete,
  225.      "Delete route."},
  226.     {OPT_TRUE, "p", (Address)&print,
  227.      "Print routes."},
  228.     {OPT_DOC, "\0", (Address)NULL, "  Interface favoring:"},
  229.     {OPT_STRING, "favor", (Address)&favorName,
  230.      "Try to install route(s) using this type of network interface."},
  231.     {OPT_STRING, "disfavor", (Address)&disfavorName,
  232.      "Try not to install route(s) using this type of network interface."},
  233.     {OPT_DOC, "\0", (Address)NULL, "\tCurrently (from first to last):"},
  234.     {OPT_DOC, "\0", (Address)NULL, "\t0. ether"},
  235.     {OPT_DOC, "\0", (Address)NULL, "\t1. fddi"},
  236.     {OPT_DOC, "\0", (Address)NULL, "\t2. ultra"},
  237.     {OPT_DOC, "\0", (Address)NULL, "  Address overriding:"},
  238.     {OPT_STRING, "ether", (Address)ðerString,
  239.      "Ethernet address for host. (ff:oo:oo:bb:aa:rr@@aa.bb.cc.dd)"},
  240.     {OPT_STRING, "ultra", (Address)&ultraString,
  241.      "Ethernet address for host. (hh/ii@@aa.bb.cc.dd)"},
  242.     {OPT_STRING, "fddi", (Address)&fddiString,
  243.      "Ethernet address for host. (qq:ww:ee:rr:tt:yy@@aa.bb.cc.dd)"},
  244.     {OPT_DOC, "\0", (Address)NULL, "  Misc:"},
  245.     {OPT_TRUE, "v", (Address)&verbose, 
  246.      "Print out verbose messages"},
  247.     {OPT_TRUE, "n", (Address)&execute, 
  248.      "Do not execute commands.  Just print what would be done."},
  249.     {OPT_STRING, "m", (Address)&machType, 
  250.      "Machine type (sun2, spur, etc.)"},
  251. };
  252. int numOptions = Opt_Number(optionArray);
  253.  
  254. /*
  255.  * In kernel module net/netTypes.h
  256.  */
  257. #ifndef NET_NUM_SPRITE_HOSTS
  258. #define NET_NUM_SPRITE_HOSTS 200
  259. #endif
  260.  
  261. /*
  262.  * This is a hack. The subnet mask should be read in from a configuration 
  263.  * file. 
  264.  */
  265.  
  266. #define SUBNET_MASK 0xFFFFFF00
  267. #define SAME_SUBNET(addr1, addr2) \
  268.     (((addr1) & SUBNET_MASK) == ((addr2) & SUBNET_MASK))
  269.  
  270.  
  271. /*
  272.  *----------------------------------------------------------------------
  273.  *
  274.  * PrintRoutes --
  275.  *
  276.  *    Print the routes.
  277.  *
  278.  * Results:
  279.  *    FAILURE if there was a problem getting the routes, SUCCESS
  280.  *      otherwise.
  281.  *
  282.  * Side effects:
  283.  *    None.
  284.  *
  285.  *----------------------------------------------------------------------
  286.  */
  287. static ReturnStatus
  288. PrintRoutes()
  289. {
  290.     Net_Route    routes[NET_NUM_SPRITE_HOSTS];
  291.     Net_Route    *ptr;
  292.     ReturnStatus status;
  293.     int          i;
  294.     int          count;
  295.     char         buffer[64];
  296.  
  297.     status = Net_GetRoutes(-1, -1, sizeof(Net_Route), routes, &count);
  298.     if (status != SUCCESS) {
  299.     printf("Net_GetRoutes returned 0x%x: %s\n", status, 
  300.         Stat_GetMsg(status));
  301.     return FAILURE;
  302.     }
  303.  
  304.     for (i = 0; i < count; i++) {
  305.     ptr = &routes[i];
  306.     if (ptr->version != NET_ROUTE_VERSION) {
  307.         fprintf(stderr, "Route `%s' has invalid version number. ", 
  308.             ptr->desc);
  309.         fprintf(stderr, "It is %d, and should be %d.\n", ptr->version,
  310.             NET_ROUTE_VERSION);
  311.         continue;
  312.     }
  313.     if (!(ptr->flags & NET_FLAGS_VALID)) {
  314.         continue;
  315.     }
  316.     printf("%3d  %-20s %-8s\n", ptr->spriteID, ptr->hostname, 
  317.         ptr->machType);
  318.     printf("\t0x%08x %-5s %-8s %s -> ", ptr->routeID, 
  319.         netProtocolNames[ptr->protocol], netTypeNames[ptr->netType],
  320.         Net_AddrToString(&ptr->interAddress, buffer));
  321.     printf("%s\n", 
  322.         Net_AddrToString(&ptr->netAddress[NET_PROTO_RAW], buffer));
  323.     printf("\tPacket (%d, %d) RPC (%d, %d)\n", 
  324.         ptr->minPacket, ptr->maxPacket, ptr->minRpc, ptr->maxRpc);
  325.     }
  326.     return SUCCESS;
  327. }
  328.  
  329.  
  330.  
  331. /*
  332.  *----------------------------------------------------------------------
  333.  *
  334.  * OnSameNetworkType --
  335.  *
  336.  *      Determine if the two hosts are on the same type of network.
  337.  *
  338.  * Results:
  339.  *      TRUE if the hosts are on the same type of network, FALSE
  340.  *      otherwise.  If TRUE, then the given Host_NetInt pointers
  341.  *      will point to the corresponding Host_NetInt structures 
  342.  *      that enable the hosts to be on that network.  Those pointers
  343.  *      are only filled in if non-NULL.
  344.  *
  345.  * Side effects:
  346.  *    None.
  347.  *
  348.  *----------------------------------------------------------------------
  349.  */
  350. static Boolean
  351. OnSameNetworkType(type, srcPtr, srcIntPtrPtr, destPtr, destIntPtrPtr)
  352.     Net_NetworkType type;
  353.     Host_Entry      *srcPtr;
  354.     Host_NetInt     **srcIntPtrPtr;  /* (out) */
  355.     Host_Entry      *destPtr;
  356.     Host_NetInt     **destIntPtrPtr;  /* (out) */
  357. {
  358.     register int i;
  359.     register int j;
  360.  
  361.     for (i = 0; i < HOST_MAX_INTERFACES; i++) {
  362.     if (srcPtr->nets[i].netAddr.type == type) {
  363.         for (j = 0; j < HOST_MAX_INTERFACES; j++) {
  364.         if (destPtr->nets[j].netAddr.type == type) {
  365.             if (srcIntPtrPtr != (Host_NetInt **)NULL) {
  366.             *srcIntPtrPtr = &srcPtr->nets[i];
  367.             }
  368.             if (destIntPtrPtr != (Host_NetInt **)NULL) {
  369.             *destIntPtrPtr = &destPtr->nets[j];
  370.             }
  371.             return TRUE;
  372.         }
  373.         }
  374.     }
  375.     }
  376.     return FALSE;
  377. }
  378.  
  379.  
  380. /*
  381.  *----------------------------------------------------------------------
  382.  *
  383.  * InterfaceByType --
  384.  *
  385.  *      Find the network interface of the host that is of the
  386.  *      given type.
  387.  *
  388.  * Results:
  389.  *      The Host_NetInt structure of the given type, or NULL if
  390.  *      the host does not have a network interface of the given type.
  391.  *
  392.  * Side effects:
  393.  *    None.
  394.  *
  395.  *----------------------------------------------------------------------
  396.  */
  397. static Host_NetInt *
  398. InterfaceByType(hostPtr, netType)
  399.     Host_Entry      *hostPtr;
  400.     Net_AddressType netType;
  401. {
  402.     Host_NetInt *netIntPtr = NULL;
  403.     int i;
  404.  
  405.     if (hostPtr == (Host_Entry *)NULL) {
  406.     return netIntPtr;
  407.     }
  408.  
  409.     for (i = 0; i < HOST_MAX_INTERFACES; i++) {
  410.     netIntPtr = &hostPtr->nets[i];
  411.     if (netIntPtr->netAddr.type == netType) {
  412.         return netIntPtr;
  413.     }
  414.     }
  415.     return (Host_NetInt *)NULL;
  416. }
  417.  
  418. /*
  419.  *----------------------------------------------------------------------
  420.  *
  421.  * EstablishInternetRoute --
  422.  *
  423.  *      Establish an internet route between two hosts.
  424.  *
  425.  * Results:
  426.  *      SUCCESS iff the route was successfully established.  In this
  427.  *      case, the given route structure is filled in with the route
  428.  *      information.  Otherwise, FAILURE is returned.
  429.  *
  430.  * Side effects:
  431.  *    None.
  432.  *
  433.  *----------------------------------------------------------------------
  434.  */
  435. static ReturnStatus
  436. EstablishInternetRoute(srcHostPtr, destHostPtr, routePtr)
  437.     Host_Entry *srcHostPtr;
  438.     Host_Entry *destHostPtr;
  439.     Net_Route  *routePtr;
  440. {
  441.     Host_NetInt         *srcIntPtr;
  442.     Host_NetInt         *destIntPtr;
  443.     Gate_Entry          *gatePtr;
  444.     ReturnStatus        status;
  445.     static Boolean    initialized = FALSE;
  446.     static int        numGates;
  447. #define MAX_GATES    5
  448.     static Gate_Entry    gates[MAX_GATES];
  449.     Boolean        found;
  450.     int            i,j;
  451.  
  452.     if (!initialized) {
  453.     /*
  454.      * Read the gateway information.
  455.      */
  456.     status = Gate_Start();
  457.     if (status != SUCCESS) {
  458.         printf("netroute: couldn't set gate file.\n");
  459.         return FAILURE;
  460.     }
  461.     numGates = 0;
  462.     gatePtr = Gate_Next();
  463.     while (gatePtr != (Gate_Entry *)NULL) {
  464.         if (numGates == MAX_GATES) {
  465.         fprintf(stderr, "netroute: too many gateways\n");
  466.         return FAILURE;
  467.         }
  468.         gates[numGates] = *gatePtr;
  469.         gatePtr = Gate_Next();
  470.         numGates++;
  471.     }
  472.     Gate_End();
  473.     initialized = TRUE;
  474.     }
  475.  
  476.     /* 
  477.      * Go through the interfaces on the local host and find one that
  478.      * is connected to a gateway.
  479.      */
  480.     found = FALSE;
  481.     for (i = 0; i < HOST_MAX_INTERFACES; i++) {
  482.     srcIntPtr = InterfaceByType(srcHostPtr, rankedInterfaceTypes[i]);
  483.     if (srcIntPtr != NULL) {
  484.         for (j = 0; j < numGates; j++) {
  485.         gatePtr = &gates[j];
  486.         if (SAME_SUBNET(Net_NetToHostInt(srcIntPtr->inetAddr), 
  487.             Net_NetToHostInt(gatePtr->inetAddr))) {
  488.             found = TRUE;
  489.             goto endloop;
  490.         }
  491.         }
  492.     }
  493.     }
  494. endloop:
  495.     if (!found) {
  496.     if (verbose) {
  497.         printf("Couldn't find a gateway to use\n");
  498.     }
  499.     return FAILURE;
  500.     }
  501.  
  502.     /*
  503.      * Pick the best interface for the destination host. 
  504.      */
  505.     for (i = 0; i < HOST_MAX_INTERFACES; i++) {
  506.     destIntPtr = InterfaceByType(destHostPtr, rankedInterfaceTypes[i]);
  507.     if (destIntPtr != NULL) {
  508.         break;
  509.     }
  510.     }
  511.     if (destIntPtr == NULL) {
  512.     if (verbose) {
  513.         printf("Couldn't find an interface to use on the destination.\n");
  514.     }
  515.     return FAILURE;
  516.     }
  517.     routePtr->version = NET_ROUTE_VERSION;
  518.     routePtr->spriteID = destHostPtr->id;
  519.     routePtr->flags = NET_FLAGS_VALID;
  520.     strcpy(routePtr->hostname, destHostPtr->aliases[0]);
  521.     strcpy(routePtr->machType, destHostPtr->machType);
  522.     routePtr->minRpc = 0;
  523.     routePtr->maxRpc = RPC_MAX_SIZE;
  524.  
  525.     /*
  526.      * The protocol for a host across a gateway is INET.  To set up the
  527.      * route's addresses correctly, the INET address gets set to the internet
  528.      * address of the ethernet network interface of the destination host.
  529.      * The RAW address gets set to the ethernet address of the gateway.
  530.      * And the route's `interAddress' gets set to the network address of
  531.      * the interface to use.
  532.      * Yahoo!  Sheer simplicity.
  533.      */
  534.     routePtr->protocol = NET_PROTO_INET;
  535.     routePtr->netAddress[NET_PROTO_RAW] = gatePtr->netAddr;
  536.     routePtr->interAddress = srcIntPtr->netAddr;
  537.     status = Net_SetAddress(NET_ADDRESS_INET, (Address) &destIntPtr->inetAddr, 
  538.         &routePtr->netAddress[NET_PROTO_INET]);
  539.     if (status != SUCCESS) {
  540.     fprintf(stderr, "netroute: Net_SetAddress failed: %s\n",
  541.         Stat_GetMsg(status));
  542.     return FAILURE;
  543.     }
  544.     status = Net_InstallRoute(sizeof(Net_Route), routePtr);
  545.  
  546.     if (status != SUCCESS) {
  547.     fprintf(stderr, "netroute: Net_InstallRoute failed (0x%x): %s\n",
  548.         status, Stat_GetMsg(status));
  549.     return FAILURE;
  550.     } else {
  551.     if (verbose) {
  552.         printf("netroute: established internet %s route to %s.\n", 
  553.            netAddressNames[destIntPtr->netAddr.type], 
  554.            *destHostPtr->aliases);
  555.     }
  556.     }
  557.     return SUCCESS;
  558. }
  559.  
  560.  
  561. /*
  562.  *----------------------------------------------------------------------
  563.  *
  564.  * EstablishLocalRoute --
  565.  *
  566.  *      Establish an local route between two hosts.
  567.  *
  568.  * Results:
  569.  *      SUCCESS iff the route was successfully established.  In this
  570.  *      case, the given route structure is filled in with the route
  571.  *      information.  Otherwise, FAILURE is returned.
  572.  *
  573.  * Side effects:
  574.  *    None.
  575.  *
  576.  *----------------------------------------------------------------------
  577.  */
  578. static ReturnStatus
  579. EstablishLocalRoute(srcHostPtr, destHostPtr, routePtr)
  580.     Host_Entry *srcHostPtr;
  581.     Host_Entry *destHostPtr;
  582.     Net_Route  *routePtr;
  583. {
  584.     register int i;
  585.     Host_NetInt  *destIntPtr = NULL;
  586.     Host_NetInt  *srcIntPtr = NULL;
  587.     ReturnStatus result;
  588.     Boolean found;
  589.  
  590.     routePtr->version = NET_ROUTE_VERSION;
  591.     routePtr->spriteID = destHostPtr->id;
  592.     routePtr->flags = NET_FLAGS_VALID;
  593.     strcpy(routePtr->hostname, destHostPtr->aliases[0]);
  594.     strcpy(routePtr->machType, destHostPtr->machType);
  595.     routePtr->minRpc = 0;
  596.     routePtr->maxRpc = RPC_MAX_SIZE;
  597.  
  598.     /*
  599.      * See if the machines are connected to the same local network.
  600.      * If so, use it.
  601.      */
  602.     found = FALSE;
  603.     for (i = 0; i < HOST_MAX_INTERFACES; i++) {
  604.     if (OnSameNetworkType(rankedInterfaceTypes[i], srcHostPtr, &srcIntPtr, 
  605.                   destHostPtr, &destIntPtr)) {
  606.         if (SAME_SUBNET(Net_NetToHostInt(srcIntPtr->inetAddr), 
  607.             Net_NetToHostInt(destIntPtr->inetAddr))) {
  608.         found = TRUE;
  609.         break;
  610.         }
  611.     }
  612.     }
  613.     if (!found) {
  614.     return FAILURE;
  615.     }
  616.     if (verbose) {
  617.     printf("%s and %s are on the same %s \n",
  618.         srcHostPtr->name, destHostPtr->name, rankedInterfaceNames[i]);
  619.     }
  620.     /*
  621.      * Local machines use the RAW protocol.  The network address of the
  622.      * destination host is placed in the route's RAW protocol slot, and
  623.      * the network address of the source host is placed in the route's
  624.      * interface address slot.
  625.      */
  626.     routePtr->protocol = NET_PROTO_RAW;
  627.     routePtr->netAddress[NET_PROTO_RAW] = destIntPtr->netAddr;
  628.     routePtr->interAddress = srcIntPtr->netAddr;
  629.  
  630.     result = Net_InstallRoute(sizeof(Net_Route), routePtr);
  631.  
  632.     if (result != SUCCESS) {
  633.     fprintf(stderr, "netroute: Net_InstallRoute failed (0x%x): %s\n",
  634.         result, Stat_GetMsg(result));
  635.     return FAILURE;
  636.     } else {
  637.     if (verbose) {
  638.         printf("netroute: established raw %s route to %s.\n", 
  639.            netAddressNames[destIntPtr->netAddr.type], 
  640.            *destHostPtr->aliases);
  641.     }
  642.     return SUCCESS;
  643.     }
  644. }
  645.  
  646.  
  647. /*
  648.  *----------------------------------------------------------------------
  649.  *
  650.  * EstablishRoute --
  651.  *
  652.  *      Establish a route between two hosts.  If the two hosts
  653.  *      are across a gateway, then an internet route is established.
  654.  *      If they are not, then a local route is established.
  655.  *
  656.  * Results:
  657.  *      SUCCESS iff the route was successfully established.  In this
  658.  *      case, the given route structure is filled in with the route
  659.  *      information.  Otherwise, FAILURE is returned.
  660.  *
  661.  * Side effects:
  662.  *    None.
  663.  *
  664.  *----------------------------------------------------------------------
  665.  */
  666. static ReturnStatus
  667. EstablishRoute(srcHostPtr, destHostPtr, routePtr)
  668.     Host_Entry *srcHostPtr;
  669.     Host_Entry *destHostPtr;
  670.     Net_Route  *routePtr;
  671. {
  672.     ReturnStatus status;
  673.  
  674.     status = EstablishLocalRoute(srcHostPtr, destHostPtr, routePtr);
  675.     if (status == FAILURE) {
  676.     status = EstablishInternetRoute(srcHostPtr, destHostPtr, routePtr);
  677.     }
  678.     return status;
  679. }
  680.  
  681.  
  682. /*
  683.  *----------------------------------------------------------------------
  684.  *
  685.  * CopyHost --
  686.  *
  687.  *      Copy the contents of the source host structure into the
  688.  *      destination host structure.
  689.  *
  690.  * Results:
  691.  *      SUCCESS iff the copy was successful, FAILURE otherwise.
  692.  *
  693.  * Side effects:
  694.  *    None.
  695.  *
  696.  *----------------------------------------------------------------------
  697.  */
  698. static ReturnStatus
  699. CopyHost(srcHostPtr, destHostPtr)
  700.     Host_Entry *srcHostPtr;
  701.     Host_Entry *destHostPtr;
  702. {
  703.     bcopy((char *) srcHostPtr, (char *) destHostPtr, sizeof(Host_Entry));
  704.     destHostPtr->name = strdup(srcHostPtr->name);
  705.     destHostPtr->aliases = (char **)malloc(sizeof(char *) * 2);
  706.     *destHostPtr->aliases = strdup(*srcHostPtr->aliases);
  707.     *(destHostPtr->aliases + 1) = NULL;
  708.     return SUCCESS;
  709. }
  710.  
  711.  
  712. /*
  713.  *----------------------------------------------------------------------
  714.  *
  715.  * EstablishLocalHost --
  716.  *
  717.  *      Determine the Host_Entry for this host, i.e., the host that
  718.  *      the routes are established from.  This information is needed
  719.  *      before routes can be established to determine whether gateways
  720.  *      are crossed, etc.
  721.  *
  722.  * Results:
  723.  *      SUCCESS iff the local host could be found, FAILURE otherwise.
  724.  *      If SUCCESS, then the given Host_Entry structure is filled in
  725.  *      with the host information.
  726.  *
  727.  * Side effects:
  728.  *    None.
  729.  *
  730.  *----------------------------------------------------------------------
  731.  */
  732. static ReturnStatus
  733. EstablishLocalHost(hostPtr)
  734.     Host_Entry *hostPtr;
  735. {
  736.     ReturnStatus     result;
  737.     int              localID;
  738.     Host_Entry       *localPtr;
  739.     Net_Route        route;
  740.     int            i;
  741.  
  742.     result = Proc_GetHostIDs(NULL, &localID);
  743.     if (result != SUCCESS) {
  744.     fprintf(stderr, "netroute: could not get this host's sprite ID.\n");
  745.     return FAILURE;
  746.     }
  747.     localPtr = Host_ByID(localID);
  748.     if (localPtr == (Host_Entry *)NULL) {
  749.     fprintf(stderr, "netroute: couldn't find local host by its ID (%d)\n",
  750.         localID);
  751.     return FAILURE;
  752.     }
  753.     if (verbose) {
  754.     printf("netroute: establishing routes from %s.\n", localPtr->name);
  755.     }
  756.     result = CopyHost(localPtr, hostPtr);
  757.     if (result != SUCCESS) {
  758.     return result;
  759.     }
  760.     localPtr = hostPtr;
  761.     /*
  762.      * This ensures that we know about ourself by giving the net module
  763.      * our internet address for each interface.
  764.      * Check in netRoute.c:Net_InstallRoute() for the details.
  765.      */
  766.     bzero((char *)&route, sizeof(Net_Route));
  767.     route.version = NET_ROUTE_VERSION;
  768.     route.spriteID = hostPtr->id;
  769.     route.flags = NET_FLAGS_VALID;
  770.     strcpy(route.hostname, hostPtr->aliases[0]);
  771.     strcpy(route.machType, hostPtr->machType);
  772.     route.minRpc = 0;
  773.     route.maxRpc = RPC_MAX_SIZE;
  774.     route.protocol = NET_PROTO_INET;
  775.     for (i = 0; i < HOST_MAX_INTERFACES; i++) {
  776.     if (hostPtr->nets[i].netAddr.type != NET_ADDRESS_NONE) {
  777.         route.netAddress[NET_PROTO_RAW] = hostPtr->nets[i].netAddr;
  778.         route.interAddress = hostPtr->nets[i].netAddr;
  779.         Net_SetAddress(NET_ADDRESS_INET, 
  780.         (Address) &hostPtr->nets[i].inetAddr, 
  781.         &route.netAddress[NET_PROTO_INET]);
  782.         result = Net_InstallRoute(sizeof(Net_Route), &route);
  783.         if (result != SUCCESS) {
  784.         fprintf(stderr, 
  785.             "netroute: Net_InstallRoute failed (0x%x): %s\n",
  786.             result, Stat_GetMsg(result));
  787.         return FAILURE;
  788.         }
  789.     }
  790.     }
  791.     return SUCCESS;
  792. }
  793.  
  794.  
  795. /*
  796.  *----------------------------------------------------------------------
  797.  *
  798.  * EstablishRoutesRange --
  799.  *
  800.  *      Establish routes to all hosts in a given range.  If the
  801.  *      endvalues of the range are both -1, then routes are established
  802.  *      to all hosts in the database file.
  803.  *
  804.  * Results:
  805.  *      SUCCESS iff the routes were established correctly, FAILURE
  806.  *      otherwise.
  807.  *
  808.  * Side effects:
  809.  *    None.
  810.  *
  811.  *----------------------------------------------------------------------
  812.  */
  813. static ReturnStatus
  814. EstablishRoutesRange(firstIDa, lastIDa)
  815.     int firstIDa;
  816.     int lastIDa;
  817. {
  818.     int result;
  819.     int count;
  820.     int firstID = firstIDa, lastID = lastIDa;
  821.     Host_Entry localHost;
  822.     Host_Entry *localPtr;
  823.     Host_Entry *hostPtr;
  824.     Net_Route route;
  825.     Net_Route oldRoutes[10];
  826.     int        i;
  827.  
  828.     localPtr = &localHost;
  829.     result = EstablishLocalHost(localPtr);
  830.     if (result != SUCCESS) {
  831.     fprintf(stderr, "netroute: couldn't establish address of this host\n");
  832.     return FAILURE;
  833.     }
  834.  
  835.     result = Host_Start();
  836.     if (result != SUCCESS) {
  837.     fprintf(stderr, "netroute: could not start host file.\n");
  838.     return FAILURE;
  839.     }
  840.     hostPtr = Host_Next();
  841.     while (hostPtr != (Host_Entry *)NULL) {
  842.     /*
  843.      * If the host is outside of the given range, skip it.
  844.      */
  845.     if ((firstID > 0) && (lastID > 0) && 
  846.         ((hostPtr->id < firstID) || (hostPtr->id > lastID))) {
  847.         hostPtr = Host_Next();
  848.         continue;
  849.     }
  850.     if (localPtr->id == hostPtr->id) {
  851.         hostPtr = Host_Next();
  852.         continue;
  853.     }
  854.     result = Net_GetRoutes(hostPtr->id, hostPtr->id, sizeof(oldRoutes), 
  855.                    oldRoutes, &count);
  856.     if (result != SUCCESS) {
  857.         count = 0;
  858.     }
  859.     route.userData = (ClientData) 0;
  860.     result = EstablishRoute(localPtr, hostPtr, &route);
  861.     if (result != SUCCESS) {
  862.         return result;
  863.     }
  864.     /*
  865.      * If we successfully established the route, then delete the
  866.      * previous ones. We only delete a previous route if its 
  867.      * userData is 0, meaning that it was installed by netroute
  868.      * from the host file. A userData value of 1 indicates that
  869.      * the route was installed by scratch (by the user) so we
  870.      * don't delete it lest the user get mad at us.
  871.      */
  872.     for (i = 0; i < count; i++) {
  873.         if (oldRoutes[i].userData == (ClientData) 0) {
  874.         if (verbose) {
  875.             printf("netroute: deleting previous route 0x%x\n", 
  876.             oldRoutes[i].routeID);
  877.         }
  878.         result = Net_DeleteRoute(oldRoutes[i].routeID);
  879.         if (result != SUCCESS) {
  880.             fprintf(stderr, "netroute: could not delete route 0x%x\n",
  881.             oldRoutes[i].routeID);
  882.             return FAILURE;
  883.         }
  884.         }
  885.     }
  886.     hostPtr = Host_Next();
  887.     }
  888.     return SUCCESS;
  889. }
  890.  
  891.  
  892. /*
  893.  *----------------------------------------------------------------------
  894.  *
  895.  * EstablishRouteScratch --
  896.  *
  897.  *      Establish a route to a host from scratch, i.e., from the
  898.  *      command line arguments.  
  899.  *
  900.  * Results:
  901.  *      SUCCESS iff the route was established correctly, FAILURE
  902.  *      otherwise.
  903.  *
  904.  * Side effects:
  905.  *    None.
  906.  *
  907.  *----------------------------------------------------------------------
  908.  */
  909. static ReturnStatus
  910. EstablishRouteScratch(spriteID)
  911.     int spriteID;
  912. {
  913.     int count;
  914.     ReturnStatus result;
  915.     Host_Entry tmpEntry;
  916.     Host_Entry *hostPtr;
  917.     Host_Entry *localPtr;
  918.     Host_Entry localHost;
  919.     Net_Route  route;
  920.  
  921.     /*
  922.      * Fill in the info for this host, and ensure that it knows
  923.      * about itself.
  924.      */
  925.     localPtr = &localHost;
  926.     result = EstablishLocalHost(localPtr);
  927.     if (result != SUCCESS) {
  928.     fprintf(stderr, "netroute: couldn't find info for local host.\n");
  929.     return FAILURE;
  930.     }
  931.     /*
  932.      * Put together the destination host from scratch.
  933.      */
  934.     hostPtr = &tmpEntry;
  935.     bzero((char *)hostPtr, sizeof(Host_Entry));
  936.     hostPtr->id = spriteID;
  937.     hostPtr->machType = machType;
  938.     hostPtr->name = strdup(hostName);
  939.     hostPtr->aliases = (char **)malloc(sizeof(char *) * 2);
  940.     *hostPtr->aliases = strdup(hostName);
  941.     *(hostPtr->aliases + 1) = NULL;
  942.     count = ManageAddressOverride(hostPtr);
  943.     if (count == 0) {
  944.     fprintf(stderr, "netroute: no addresses specified for installing a ");
  945.     fprintf(stderr, "route from scratch.\n");
  946.     return FAILURE;
  947.     }
  948.     route.userData = (ClientData) 1;
  949.     result = EstablishRoute(localPtr, hostPtr, &route);
  950.     return result;
  951. }
  952.  
  953.  
  954. /*
  955.  *----------------------------------------------------------------------
  956.  *
  957.  * DeleteRouteByID --
  958.  *
  959.  *      Delete the route denoted by its ID.
  960.  *
  961.  * Results:
  962.  *      SUCCESS iff the route was deleted correctly, FAILURE
  963.  *      otherwise.
  964.  *
  965.  * Side effects:
  966.  *    None.
  967.  *
  968.  *----------------------------------------------------------------------
  969.  */
  970. static ReturnStatus
  971. DeleteRouteByID(routeID)
  972.     int routeID;
  973. {
  974.     ReturnStatus status;
  975.  
  976.     if (verbose) {
  977.     printf("netroute: deleting route 0x%x/0x%x.\n", routeID,
  978.            routeID >> 16);
  979.     }
  980.     status = Net_DeleteRoute(routeID);
  981.     if (status != SUCCESS) {
  982.     printf("Net_DeleteRoute returned 0x%x: %s\n", status,
  983.            Stat_GetMsg(status));
  984.     return FAILURE;
  985.     }
  986.     return SUCCESS;
  987. }
  988.  
  989.  
  990. /*
  991.  *----------------------------------------------------------------------
  992.  *
  993.  * DeleteRoutesRange --
  994.  *
  995.  *      Delete the routes to the hosts whose Sprite IDs fall within
  996.  *      a given range.
  997.  *
  998.  * Results:
  999.  *      SUCCESS iff the routes were deleted correctly, FAILURE
  1000.  *      otherwise.
  1001.  *
  1002.  * Side effects:
  1003.  *    None.
  1004.  *
  1005.  *----------------------------------------------------------------------
  1006.  */
  1007. static ReturnStatus
  1008. DeleteRoutesRange(firstID, lastID)
  1009.     int firstID;
  1010.     int lastID;
  1011. {
  1012.     int          count;
  1013.     int          i;
  1014.     ReturnStatus status;
  1015.     Net_Route    routes[NET_NUM_SPRITE_HOSTS];
  1016.  
  1017.     status = Net_GetRoutes(firstID, lastID, sizeof(Net_Route), routes, &count);
  1018.     if (status != SUCCESS) {
  1019.     fprintf(stderr, "Net_GetRoutes returned 0x%x: %s\n", status, 
  1020.         Stat_GetMsg(status));
  1021.     return FAILURE;
  1022.     }
  1023.     for (i = 0; i < count; i++) {
  1024.     status = Net_DeleteRoute(routes[i].routeID);
  1025.     if (status != SUCCESS) {
  1026.         printf("Net_DeleteRoute returned 0x%x: %s\n", status,
  1027.            Stat_GetMsg(status));
  1028.         return FAILURE;
  1029.     }
  1030.     }
  1031.     return SUCCESS;
  1032. }
  1033.  
  1034.  
  1035. /*
  1036.  *----------------------------------------------------------------------
  1037.  *
  1038.  * SplitOffInternetAddress --
  1039.  *
  1040.  *      Take a command line argument of the form
  1041.  *      "net_address@@internet_address", NULL the '@@', and
  1042.  *      return a pointer to the "internet_address".
  1043.  *      
  1044.  *
  1045.  * Results:
  1046.  *      A pointer to the "internet_address", or NULL if it could not
  1047.  *      be found.
  1048.  *
  1049.  * Side effects:
  1050.  *    None.
  1051.  *
  1052.  *----------------------------------------------------------------------
  1053.  */
  1054. static char *
  1055. SplitOffInternetAddress(addrString)
  1056.     register char *addrString;
  1057. {
  1058.     while (*addrString != '\0') {
  1059.     if (*addrString == '@@') {
  1060.         *addrString  = '\0';
  1061.         return (addrString + 1);
  1062.     }
  1063.     addrString++;
  1064.     }
  1065.     return NULL;
  1066. }
  1067.  
  1068.  
  1069. /*
  1070.  *----------------------------------------------------------------------
  1071.  *
  1072.  * ManageAddressOverride --
  1073.  *
  1074.  *      Take the addresses given on the command line and convert
  1075.  *      them into Host_NetInt structures for the given host.
  1076.  *
  1077.  * Results:
  1078.  *      The number of network addresses converted from the command
  1079.  *      line.
  1080.  *      
  1081.  *
  1082.  * Side effects:
  1083.  *    None.
  1084.  *
  1085.  *----------------------------------------------------------------------
  1086.  */
  1087. static int
  1088. ManageAddressOverride(hostPtr)
  1089.     Host_Entry *hostPtr;
  1090. {
  1091.     char *inetStr;
  1092.     int index = 0;
  1093.     Host_NetInt *netIntPtr;
  1094.     ReturnStatus result;
  1095.  
  1096.     if (etherString != NULL) {
  1097.     netIntPtr = &hostPtr->nets[index];
  1098.     inetStr = SplitOffInternetAddress(etherString);
  1099.     result = Net_StringToAddr(etherString, NET_ADDRESS_ETHER, 
  1100.                      &netIntPtr->netAddr);
  1101.     if (result != SUCCESS) {
  1102.         fprintf(stderr, "netroute: couldn't convert %s to an ethernet ",
  1103.             etherString);
  1104.         fprintf(stderr, "address for host %d.\n", hostPtr->id);
  1105.         return 0;
  1106.     }
  1107.     netIntPtr->inetAddr = Net_StringToInetAddr(inetStr);
  1108.     index++;
  1109.     }
  1110.     if (ultraString != NULL) {
  1111.     netIntPtr = &hostPtr->nets[index];
  1112.     inetStr = SplitOffInternetAddress(ultraString);
  1113.     result = Net_StringToAddr(ultraString, NET_ADDRESS_ULTRA, 
  1114.                      &netIntPtr->netAddr);
  1115.     if (result != SUCCESS) {
  1116.         fprintf(stderr, "netroute: couldn't convert %s to an ultranet ",
  1117.             ultraString);
  1118.         fprintf(stderr, "address for host %d.\n", hostPtr->id);
  1119.         return 0;
  1120.     }
  1121.     netIntPtr->inetAddr = Net_StringToInetAddr(inetStr);
  1122.     index++;
  1123.     }
  1124.     if (fddiString != NULL) {
  1125.     netIntPtr = &hostPtr->nets[index];
  1126.     inetStr = SplitOffInternetAddress(fddiString);
  1127.     result = Net_StringToAddr(fddiString, NET_ADDRESS_FDDI, 
  1128.                      &netIntPtr->netAddr);
  1129.     if (result != SUCCESS) {
  1130.         fprintf(stderr, "netroute: couldn't convert %s to an FDDI ",
  1131.             fddiString);
  1132.         fprintf(stderr, "address for host %d.\n", hostPtr->id);
  1133.         return 0;
  1134.     }
  1135.     netIntPtr->inetAddr = Net_StringToInetAddr(inetStr);
  1136.     index++;
  1137.     }
  1138.     return index;
  1139. }
  1140.  
  1141.  
  1142. /*
  1143.  *----------------------------------------------------------------------
  1144.  *
  1145.  * ManageFavorings --
  1146.  *
  1147.  *      Juggle the list of network types according to the favorings
  1148.  *      in the command line.  The program is compiled with a default
  1149.  *      list of network types that it will traverse when trying to
  1150.  *      establish routes to a host, and it will establish the first one
  1151.  *      that it can.  The user can change the list ordering
  1152.  *      using the command line to change this behavior.
  1153.  *
  1154.  * Results:
  1155.  *      None.
  1156.  *
  1157.  * Side effects:
  1158.  *    None.
  1159.  *
  1160.  *----------------------------------------------------------------------
  1161.  */
  1162. static void
  1163. ManageFavorings()
  1164. {
  1165.     register int    i;
  1166.     register int    j;
  1167.     register char   *ptr;
  1168.     Net_NetworkType type;
  1169.  
  1170.     if (favorName != NULL) {
  1171.     for (i = 0; i < HOST_MAX_INTERFACES; i++) {
  1172.         if (!strcmp(favorName, rankedInterfaceNames[i])) {
  1173.         ptr = rankedInterfaceNames[i];
  1174.         type = rankedInterfaceTypes[i];
  1175.         for (j = i - 1; j >= 0; j--) {
  1176.             rankedInterfaceNames[j + 1] = rankedInterfaceNames[j];
  1177.             rankedInterfaceTypes[j + 1] = rankedInterfaceTypes[j];
  1178.         }
  1179.         rankedInterfaceNames[0] = ptr;
  1180.         rankedInterfaceTypes[0] = type;
  1181.         break;
  1182.         }
  1183.     }
  1184.     }
  1185.     if (disfavorName != NULL) {
  1186.     for (i = 0; i < HOST_MAX_INTERFACES; i++) {
  1187.         if (!strcmp(disfavorName, rankedInterfaceNames[i])) {
  1188.         ptr = rankedInterfaceNames[i];
  1189.         type = rankedInterfaceTypes[i];
  1190.         for (j = i + 1; j < HOST_MAX_INTERFACES; j++) {
  1191.             rankedInterfaceNames[j - 1] = rankedInterfaceNames[j];
  1192.             rankedInterfaceTypes[j - 1] = rankedInterfaceTypes[j];
  1193.         }
  1194.         rankedInterfaceNames[HOST_MAX_INTERFACES - 1] = ptr;
  1195.         rankedInterfaceTypes[HOST_MAX_INTERFACES - 1] = type;
  1196.         break;
  1197.         }
  1198.     }
  1199.     }
  1200.     if (verbose) {
  1201.     printf("netroute: Interface Rankings: ");
  1202.     for (i = 0; i < HOST_MAX_INTERFACES; i++) {
  1203.         printf("%d. %s ", i, rankedInterfaceNames[i]);
  1204.     }
  1205.     printf("\n");
  1206.     }                   
  1207. }
  1208.  
  1209.  
  1210. /*
  1211.  *----------------------------------------------------------------------
  1212.  *
  1213.  * main --
  1214.  *
  1215.  *      Establish, delete, or print routes to specified hosts.
  1216.  *
  1217.  * Results:
  1218.  *      None.
  1219.  *
  1220.  * Side effects:
  1221.  *    None.
  1222.  *
  1223.  *----------------------------------------------------------------------
  1224.  */
  1225. int
  1226. main(argc, argv)
  1227.     int argc;
  1228.     char *argv[];
  1229. {
  1230.     ReturnStatus result;
  1231.     Host_Entry   *entryPtr;
  1232.  
  1233.     argc = Opt_Parse(argc, argv, optionArray, numOptions, 0);
  1234.     if (first < 0 && last > -1) {
  1235.     first = 0;
  1236.     }
  1237.     if (last < 0 && first > -1) {
  1238.     last = NET_NUM_SPRITE_HOSTS;
  1239.     }
  1240.     if (last < first) {
  1241.     fprintf(stderr, "netroute: `last' (%d) less than `first' (%d)\n", 
  1242.         last, first);
  1243.     exit(1);
  1244.     }
  1245.  
  1246.     if (print) {
  1247.     install = FALSE;
  1248.     }
  1249.     if (inputFilename != NULL) {
  1250.     Host_SetFile(inputFilename);
  1251.     result = Host_Start();
  1252.     if (result != SUCCESS) {
  1253.         fprintf(stderr, "netroute: could not open host file %s.\n",
  1254.             inputFilename);
  1255.         exit(1);
  1256.     }
  1257.     }
  1258.  
  1259.     if (hostName != NULL) {
  1260.     entryPtr = Host_ByName(hostName);
  1261.     if (entryPtr == (Host_Entry *)NULL) {
  1262.         hostID = atoi(hostName);
  1263.         entryPtr = Host_ByID(hostID);
  1264.         if (entryPtr == (Host_Entry *)NULL) {
  1265.         fprintf(stderr, "netroute: could not resolve host %s\n",
  1266.             hostName);
  1267.         exit(1);
  1268.         }
  1269.         hostName = strdup(entryPtr->name);
  1270.     } else {
  1271.         hostID = entryPtr->id;
  1272.     }
  1273.     }
  1274.  
  1275.     /*
  1276.      * If a host is specified, then it overrides a range.
  1277.      */
  1278.     if (hostID > 0) {
  1279.     first = hostID;
  1280.     last = hostID;
  1281.     }
  1282.  
  1283.     if (delete != -1) {
  1284.     result = DeleteRouteByID(delete);
  1285.     if (result != SUCCESS) {
  1286.         fprintf(stderr, "netroute: failed to delete routes.\n");
  1287.         exit(1);
  1288.     } else {
  1289.         printf("netroute: routes deleted.\n");
  1290.     }
  1291.     print = FALSE;
  1292.     }
  1293.  
  1294.     if (install) {
  1295.     ManageFavorings();
  1296.     if (etherString != NULL || ultraString != NULL || fddiString != NULL) {
  1297.         result = EstablishRouteScratch(hostID);
  1298.     } else {
  1299.         result = EstablishRoutesRange(-1, -1);
  1300.     }
  1301.     if (result != SUCCESS) {
  1302.         fprintf(stderr, "netroute: failed installing routes.\n");
  1303.         exit(1);
  1304.     } else {
  1305.         printf("netroute: routes installed.\n");
  1306.     }
  1307.     print = FALSE;
  1308.     }
  1309.  
  1310.     if (print) {
  1311.     (void) PrintRoutes();
  1312.     }
  1313.     exit(0);
  1314. }    
  1315. @
  1316.  
  1317.  
  1318. 1.20
  1319. log
  1320. @print vs install was broken
  1321. @
  1322. text
  1323. @d19 1
  1324. a19 1
  1325. static char rcsid[] = "$Header: /sprite/src/cmds/netroute/RCS/netroute.c,v 1.19 92/06/23 11:37:52 jhh Exp $ SPRITE (Berkeley)";
  1326. d61 1
  1327. a61 1
  1328. char   *inputFilename = "/etc/spritehosts.new";
  1329. @
  1330.  
  1331.  
  1332. 1.19
  1333. log
  1334. @cleaned up some lint
  1335. @
  1336. text
  1337. @d19 1
  1338. a19 1
  1339. static char rcsid[] = "$Header: /sprite/src/cmds/netroute/RCS/netroute.c,v 1.18 92/06/23 11:31:04 jhh Exp Locker: jhh $ SPRITE (Berkeley)";
  1340. d1117 3
  1341. @
  1342.  
  1343.  
  1344. 1.18
  1345. log
  1346. @uses new Host library, does deletion
  1347. @
  1348. text
  1349. @d19 1
  1350. a19 1
  1351. static char rcsid[] = "$Header: /sprite/src/cmds/netroute.new/RCS/netroute.c,v 1.1 92/06/05 13:08:35 jhh Exp Locker: jhh $ SPRITE (Berkeley)";
  1352. a57 4
  1353. Net_Address etherAddress;
  1354. Net_Address ultraAddress;
  1355. Net_Address fddiAddress;
  1356.  
  1357. a77 1
  1358. char   *inetString = NULL;
  1359. a81 1
  1360. int     routeID = -1;
  1361. a314 1
  1362.     Net_Address         netAddr;
  1363. d408 1
  1364. a408 1
  1365.     status = Net_SetAddress(NET_ADDRESS_INET, &destIntPtr->inetAddr, 
  1366. d574 1
  1367. a574 1
  1368.     bcopy(srcHostPtr, destHostPtr, sizeof(Host_Entry));
  1369. a610 2
  1370.     int              count = 0;
  1371.     Host_NetInt         *intPtr;
  1372. a690 1
  1373.     int routeID;
  1374. d813 1
  1375. a813 1
  1376.     count = ManageAddressOverride();
  1377. a845 1
  1378.     Net_Route    route;
  1379. a1101 1
  1380.     int          usingHostfile = FALSE;
  1381. a1124 1
  1382.     usingHostfile = TRUE;
  1383. d1165 1
  1384. a1165 1
  1385.         result = EstablishRouteScratch();
  1386. @
  1387.  
  1388.  
  1389. 1.17
  1390. log
  1391. @added access to FDDI routes...
  1392. @
  1393. text
  1394. @d19 1
  1395. a19 1
  1396. static char rcsid[] = "$Header: /sprite/src/cmds/netroute/RCS/netroute.c,v 1.16 92/02/14 19:11:29 jhh Exp Locker: voelker $ SPRITE (Berkeley)";
  1397. d34 1
  1398. d40 1
  1399. d42 20
  1400. d65 19
  1401. a83 15
  1402. Boolean table = FALSE;
  1403. Boolean keepDomain = FALSE;
  1404. int hostID = -1;
  1405. char *hostName;
  1406. char *etherString;
  1407. char *inetString;
  1408. char *inputFile;
  1409. char *machType;
  1410. char *netTypeString = "ether";
  1411. char *ultraString = NULL;
  1412. char *fddiString = NULL;
  1413. Boolean debug = FALSE;
  1414. unsigned int interval = 105;
  1415. unsigned int range = 30;
  1416. Boolean daemon = FALSE;
  1417. d85 4
  1418. a88 1
  1419. char *output = NULL;
  1420. d91 3
  1421. a93 8
  1422.     {OPT_STRING, "f", (Address)&inputFile, "Specifies the host database file."},
  1423.     {OPT_TRUE, "p", (Address)&table, "Print out the route table"},
  1424.     {OPT_TRUE, "D", (Address)&keepDomain, "Keep domain suffix on host name"},
  1425.     {OPT_TRUE, "k", (Address)&debug, "Print debugging information"},
  1426.     {OPT_TRUE, "d", (Address)&daemon, 
  1427.         "Run as a daemon, downloading routes when the database changes."},
  1428.     {OPT_INT, "T", (Address)&interval, 
  1429.         "Set the interval for the daemon to check the database (seconds)."},
  1430. d95 33
  1431. a127 18
  1432.         "Install route for Sprite host with given name or Sprite ID"},
  1433.     {OPT_STRING, "i", (Address)&inetString, 
  1434.         "Internet address for route. (aa.bb.cc.dd)"},
  1435.     {OPT_STRING, "e", (Address)ðerString, 
  1436.         "Ethernet address for host. (aa:bb:cc:dd:ee:ff)"},
  1437.     {OPT_STRING, "u", (Address) &ultraString, 
  1438.         "Ultranet address for host. (a/b)"},
  1439.     {OPT_STRING, "f", (Address) &fddiString, 
  1440.         "FDDI address for host. (qq:ww:ee:rr:tt:yy)"},
  1441.     {OPT_INT, "n", (Address)&hostID, "Sprite ID for host."},
  1442.     {OPT_STRING, "t", (Address)&netTypeString, "Specifies the type of route."},
  1443.     {OPT_STRING, "m", (Address)&machType, "Machine type (sun2, spur, etc.)"},
  1444.     {OPT_INT, "r", (Address)&range, 
  1445.     "Range of random values to add to interval."},
  1446.     {OPT_TRUE, "v", (Address)&verbose, "Print out verbose messages"},
  1447.     {OPT_STRING, "o", (Address)&output, "Output file for daemon."},
  1448.     {OPT_DOC, "\0", (Address)NULL, "If -h is not specified, routes are installed for all hosts in"},
  1449.     {OPT_DOC, "\0", (Address)NULL, "the host database."},
  1450. d131 15
  1451. a145 16
  1452. void            FixHostName _ARGS_((char *charPtr));
  1453. ReturnStatus        InstallRoute _ARGS_((Host_Entry *hostPtr, 
  1454.                 Address addressData, int netType));
  1455. ReturnStatus        DownloadFile _ARGS_((void));
  1456. void            Daemon _ARGS_((unsigned int interval));
  1457.  
  1458. extern    ReturnStatus    Proc_GetHostIDs _ARGS_((int *vID, int *pID));
  1459. extern    void        sleep _ARGS_((unsigned seconds));
  1460.  
  1461. char    *myname;
  1462.  
  1463. /*
  1464.  * These guys are global because Download() accesses them. 
  1465.  */
  1466. Net_UltraAddress    *ultraAddressPtr = NULL;
  1467. Net_FDDIAddress         *fddiAddressPtr = (Net_FDDIAddress *) NULL;
  1468. d151 7
  1469. a157 1
  1470.  * main --
  1471. d159 2
  1472. a160 1
  1473.  *    Grab command line arguments and install routes.
  1474. d164 41
  1475. d206 28
  1476. a233 4
  1477. int
  1478. main(argc, argv)
  1479.     int    argc;
  1480.     char *argv[];
  1481. d235 16
  1482. a250 38
  1483.     ReturnStatus    status = SUCCESS;
  1484.     Host_Entry        *host;
  1485.     Host_Entry        tmpEntry;
  1486.     Host_NetType    netType = HOST_ETHER;
  1487.     Net_EtherAddress    *etherAddressPtr = (Net_EtherAddress *) 0;
  1488.     Net_InetAddress    inetAddress = 0;
  1489.     extern int        errno;
  1490.     static Net_Address    netAddress;
  1491.     Net_RouteInfo    routeInfo;
  1492.     Boolean        newHost = FALSE;
  1493.  
  1494.     myname = argv[0];
  1495.     argc = Opt_Parse(argc, argv, optionArray, numOptions, OPT_ALLOW_CLUSTERING);
  1496.     /*
  1497.      * Startup the host file.
  1498.      */
  1499.     if (inputFile != (char *)0) {
  1500.     if (Host_SetFile(inputFile) < 0) {
  1501.         perror("Host_SetFile");
  1502.         exit(errno);
  1503.     }
  1504.     Host_End();
  1505.     }
  1506.     routeInfo.version = NET_ROUTE_VERSION;
  1507.     bzero((char *) &netAddress, sizeof(netAddress));
  1508.     /*
  1509.      * Translate and validate the etherString, inetString, and netTypeString
  1510.      * arguments.
  1511.      */
  1512.     if (etherString != (char *) 0) {
  1513.     static Net_EtherAddress etherAddress;
  1514.     static Net_EtherAddress zeroAddress;
  1515.     etherAddressPtr = ðerAddress;
  1516.     Net_StringToEtherAddr(etherString, etherAddressPtr);
  1517.     if (NET_ETHER_COMPARE(zeroAddress,etherAddress)) {
  1518.         fprintf(stderr,"netroute: Malformed ether address %s\n",
  1519.             etherString); 
  1520.         exit(1);
  1521. d253 28
  1522. d282 8
  1523. a289 6
  1524.     if (inetString != (char *) 0) {
  1525.     inetAddress = Net_StringToInetAddr(inetString);
  1526.     if (inetAddress == NET_INET_BROADCAST_ADDR) {
  1527.         fprintf(stderr,"netroute: Malformed inet address %s\n",
  1528.             inetString); 
  1529.         exit(1);
  1530. d292 43
  1531. a334 4
  1532.     if (ultraString != NULL) {
  1533.     ultraAddressPtr = &netAddress.ultra;
  1534.     status = Net_StringToAddr(ultraString, NET_PROTO_RAW, NET_NETWORK_ULTRA,
  1535.             &netAddress);
  1536. d336 40
  1537. a375 3
  1538.         fprintf(stderr, "netroute: malformed ultranet address %s\n",
  1539.         ultraString);
  1540.         exit(1);
  1541. d377 1
  1542. a378 2
  1543.     if (fddiString != NULL) {
  1544.     static Net_FDDIAddress zeroAddress;
  1545. d380 12
  1546. a391 6
  1547.     fddiAddressPtr = &netAddress.fddi;
  1548.     Net_StringToEtherAddr(fddiString, fddiAddressPtr);
  1549.     if (NET_FDDI_COMPARE(zeroAddress, *fddiAddressPtr)) {
  1550.         fprintf(stderr,"netroute: Malformed FDDI address %s\n",
  1551.             fddiString); 
  1552.         exit(1);
  1553. d393 28
  1554. d422 6
  1555. a427 12
  1556.     if (strcmp(netTypeString, "ether") == 0) {
  1557.     netType = HOST_ETHER;
  1558.     } else if (strcmp(netTypeString, "inet") == 0) {
  1559.     netType = HOST_INET;
  1560.     } else if (strcmp(netTypeString, "ultra") == 0) {
  1561.     /* 
  1562.      * TODO: fill this in.
  1563.      */
  1564.     } else if (strcmp(netTypeString, "fddi") == 0) {
  1565.     /* 
  1566.      * TODO: fill this in.
  1567.      */
  1568. d429 5
  1569. a433 2
  1570.     fprintf(stderr, "netroute: Bad net type '%s', must be 'ether' or 'inet'.\n", 
  1571.         netTypeString);
  1572. d435 2
  1573. d438 37
  1574. a474 8
  1575.     if (table) {
  1576.     /*
  1577.      * Print out the route table.
  1578.      */
  1579.     Net_RouteInfo route;
  1580.     int spriteID;
  1581.     char    buffer[100];
  1582.     char    *typePtr;
  1583. d476 11
  1584. a486 6
  1585.     printf("Sprite Route Table:\n");
  1586.     Host_Start();
  1587.     for (spriteID = 0; ; spriteID++) {
  1588.  
  1589.         status = Sys_Stats(SYS_NET_GET_ROUTE, spriteID, &route);
  1590.         if (status != SUCCESS) {
  1591. d489 179
  1592. a667 2
  1593.         if (route.version != NET_ROUTE_VERSION) {
  1594.         exit(1);
  1595. a668 43
  1596.         if (!(route.flags & NET_FLAGS_VALID)) {
  1597.         continue;
  1598.         }
  1599.         host = Host_ByID(spriteID);
  1600.         if (host && !keepDomain) {
  1601.         FixHostName(host->name);
  1602.         }
  1603.         printf("%5d %-20s", spriteID, 
  1604.         (host != NULL ? host->name :
  1605.             (spriteID == NET_BROADCAST_HOSTID ? "BROADCAST" : "???")));
  1606.         printf("%-8s", (host != NULL ? host->machType :
  1607.             (spriteID == NET_BROADCAST_HOSTID ? "" : "???")));
  1608.         switch(route.netType) {
  1609.         case NET_NETWORK_ETHER: 
  1610.             typePtr = "ether";
  1611.             break;
  1612.         case NET_NETWORK_ULTRA:
  1613.             typePtr = "ultra";
  1614.             break;
  1615.         case NET_NETWORK_FDDI:
  1616.             typePtr = "fddi";
  1617.             break;
  1618.         default:
  1619.             typePtr = "unknown";
  1620.         }
  1621.         printf("%-8s", typePtr);
  1622.         (void) Net_AddrToString(&route.netAddress[NET_PROTO_RAW],
  1623.             NET_PROTO_RAW, route.netType, buffer);
  1624.         printf("%s\n", buffer);
  1625.         switch(route.protocol) {
  1626.         case NET_PROTO_RAW: 
  1627.             break;
  1628.         case NET_PROTO_INET: 
  1629.             (void) Net_AddrToString(&route.netAddress[NET_PROTO_INET],
  1630.             NET_PROTO_INET, route.netType, buffer);
  1631.             printf("%34s%-8s%s\n", "", "inet", buffer);
  1632.             break;
  1633.         default:
  1634.             printf("Unknown protocol %d\n", route.protocol);
  1635.             break;
  1636.         }
  1637.         printf("%10s flags = 0x%x, refCount = %d, maxBytes = %d\n", "", 
  1638.             route.flags, route.refCount, route.maxBytes);
  1639. d670 45
  1640. a714 18
  1641.     Host_End();
  1642.     if (status != SUCCESS) {
  1643.         exit(1);
  1644.     }
  1645.     exit(0);
  1646.     } 
  1647.     if (daemon) {
  1648.     Daemon(interval, range);
  1649.     } else if (hostName == (char *)0) {
  1650.     DownloadFile();
  1651.     }  else {
  1652.     /*
  1653.      * Otherwise, Install a route for a single Sprite host. See if 
  1654.      * we can find the default values of the route from the Host 
  1655.      * database.  We know the user specified either a hostID or 
  1656.      * hostName so we lookup in the Host database until we find it.
  1657.      */
  1658.     Host_Start();
  1659. d716 7
  1660. d724 1
  1661. a724 1
  1662.      * First see if it is a valid hostname if not a name then a number.
  1663. d726 18
  1664. a743 4
  1665.     host = Host_ByName(hostName);
  1666.     if (host == (Host_Entry *)NULL) {
  1667.         hostID = atoi(hostName);
  1668.         host = Host_ByID(hostID);
  1669. d746 115
  1670. a860 64
  1671.      * Fill in hostID, hostName, and etherAddress letting the user
  1672.      * specified values override the values in the data base.
  1673.      */
  1674.     if (hostID < 0) {
  1675.         if (host == (Host_Entry *)NULL) {
  1676.         fprintf(stderr, "netroute: Can not compute spriteID for %s\n",
  1677.              hostName);
  1678.          status = FAILURE;
  1679.         }
  1680.         hostID = host->id;
  1681.        }
  1682.        if (host == NULL) {
  1683.         host = &tmpEntry;
  1684.         bzero((char *) host , sizeof(host));
  1685.         newHost = TRUE;
  1686.        }
  1687.        if (hostName != (char *) 0) {
  1688.        host->name = hostName;
  1689.        } else if (newHost) {
  1690.         static char nameBuffer[64];
  1691.         sprintf(nameBuffer, "host%d", hostID);
  1692.         host->name = nameBuffer;
  1693.     }
  1694.     
  1695.        if (etherAddressPtr != NULL) {
  1696.        bcopy((char *) etherAddressPtr, host->netAddr.etherAddr, 
  1697.            HOST_ETHER_ADDRESS_SIZE);
  1698.        } else if (newHost) {
  1699.         fprintf(stderr, "%s: Need -e option.\n", argv[0]);
  1700.         status = FAILURE;
  1701.        }
  1702.        if (machType != NULL) {
  1703.       host->machType = machType;
  1704.        } else if (newHost) {
  1705.         fprintf(stderr, "%s: Need -e option.\n", argv[0]);
  1706.         status = FAILURE;
  1707.        }
  1708.        if (inetAddress != 0) {
  1709.       bcopy(&inetAddress, &(host->inetAddr), sizeof(host->inetAddr));
  1710.        } else if (newHost && (netType == HOST_INET)) {
  1711.         fprintf(stderr, "netroute: Need -i option.\n");
  1712.         status = FAILURE;
  1713.     }
  1714.        if (!keepDomain) {
  1715.         FixHostName(host->name);
  1716.        }
  1717.        if (status == SUCCESS) {
  1718.        if (etherAddressPtr != NULL) {
  1719.            status = InstallRoute(host, NULL,
  1720.                      NET_NETWORK_ETHER);
  1721.        } else if (ultraAddressPtr != NULL) {
  1722.            status = InstallRoute(host, (Address) ultraAddressPtr, 
  1723.                      NET_NETWORK_ULTRA);
  1724.        } else if (fddiAddressPtr != NULL) {
  1725.            status = InstallRoute(host, (Address) fddiAddressPtr,
  1726.                      NET_NETWORK_FDDI);
  1727.        } else {
  1728.            status = FAILURE;
  1729.        }
  1730.        if (status != SUCCESS) {
  1731.            printf("Couldn't install route to host %s : %s\n", 
  1732.               host->name, Stat_GetMsg(status));
  1733.        }
  1734.        }
  1735. d862 1
  1736. a862 1
  1737.     Host_End();
  1738. d864 3
  1739. a866 1
  1740.     exit(1);
  1741. d868 1
  1742. a868 1
  1743.    return 0;
  1744. d870 1
  1745. d875 1
  1746. a875 1
  1747.  * FixHostName --
  1748. d877 2
  1749. a878 1
  1750.  *    Remove the domain part from a host name.
  1751. d881 2
  1752. a882 1
  1753.  *    None.
  1754. d885 1
  1755. a885 1
  1756.  *    String is modified in place.
  1757. d889 4
  1758. a892 4
  1759.  
  1760. void
  1761. FixHostName(charPtr)
  1762.      register char *charPtr;
  1763. d894 4
  1764. d899 12
  1765. a910 4
  1766.     for (; *charPtr != '\0' ; charPtr++) {
  1767.     if (*charPtr == '.') {
  1768.         *charPtr = '\0';
  1769.         break;
  1770. d913 1
  1771. d915 1
  1772. d920 1
  1773. a920 1
  1774.  * InstallRoute --
  1775. d922 4
  1776. a925 1
  1777.  *    description.
  1778. d928 16
  1779. a943 56
  1780.  *    None.
  1781.  *
  1782.  * Side effects:
  1783.  *    None.
  1784.  *
  1785.  *----------------------------------------------------------------------
  1786.  */
  1787.  
  1788. ReturnStatus
  1789. InstallRoute(hostPtr, addressData, netType)
  1790.     Host_Entry        *hostPtr;
  1791.     Address             addressData;
  1792.     int                 netType;
  1793. {
  1794.     Net_RouteInfo    routeInfo;
  1795.     ReturnStatus    status = SUCCESS;
  1796.     Net_InetAddress    inetAddress;
  1797.     Net_UltraAddress    *ultraAddressPtr;
  1798.     Net_FDDIAddress     *fddiAddressPtr;
  1799.  
  1800.     bzero((char *) &routeInfo, sizeof(routeInfo));
  1801.     routeInfo.spriteID = hostPtr->id;
  1802.     routeInfo.version = NET_ROUTE_VERSION;
  1803.     /*
  1804.      * This is kind of a hack.  We have to be smarter about which
  1805.      * interface the route is for.
  1806.      */
  1807.     routeInfo.interface = 0;
  1808.     strncpy(routeInfo.hostname, hostPtr->name, sizeof(routeInfo.hostname));
  1809.     routeInfo.hostname[sizeof(routeInfo.hostname)-1] = '\0';
  1810.     strncpy(routeInfo.machType, hostPtr->machType, sizeof(routeInfo.machType));
  1811.     routeInfo.machType[sizeof(routeInfo.machType)-1] = '\0';
  1812.  
  1813.     switch(netType) {
  1814.     case NET_NETWORK_ETHER:
  1815.     hostPtr = (Host_Entry *)addressData;
  1816.     routeInfo.netType = NET_NETWORK_ETHER;
  1817.     if (hostPtr->netType == HOST_ETHER) {
  1818.         routeInfo.protocol = NET_PROTO_RAW;
  1819.         /*
  1820.          * This is a hack because the types don't match.
  1821.          */
  1822.         bcopy(&hostPtr->netAddr.etherAddr,
  1823.             &routeInfo.netAddress[NET_PROTO_RAW].ether, 6);
  1824.     } else if (hostPtr->netType == HOST_INET) {
  1825.     
  1826.         routeInfo.protocol = NET_PROTO_INET;
  1827.         bcopy(&hostPtr->netAddr.etherAddr,
  1828.             &routeInfo.netAddress[NET_PROTO_RAW].ether, 6);
  1829.         bcopy(&(hostPtr->inetAddr), &inetAddress, sizeof(hostPtr->inetAddr));
  1830.         inetAddress = Net_HostToNetInt(inetAddress);
  1831.         routeInfo.netAddress[NET_PROTO_INET].inet = inetAddress;
  1832.     } else {
  1833.         fprintf(stderr, "Unknown net type %d for %s\n",
  1834.             hostPtr->netType, hostPtr->name);
  1835.         return status;
  1836. d945 1
  1837. a945 16
  1838.     break;
  1839.     case NET_NETWORK_ULTRA:
  1840.     ultraAddressPtr = (Net_UltraAddress *)addressData;
  1841.     routeInfo.netType = NET_NETWORK_ULTRA;
  1842.     routeInfo.netAddress[NET_PROTO_RAW].ultra = *ultraAddressPtr;
  1843.     routeInfo.protocol = NET_PROTO_RAW;
  1844.     break;
  1845.     case NET_NETWORK_FDDI:
  1846.     fddiAddressPtr = (Net_FDDIAddress *)addressData;
  1847.     routeInfo.netType = NET_NETWORK_FDDI;
  1848.     routeInfo.netAddress[NET_PROTO_RAW].fddi = *fddiAddressPtr;
  1849.     routeInfo.protocol = NET_PROTO_RAW;
  1850.     break;
  1851.     default:
  1852.     fprintf(stderr, "netroute: unknown network type in InstallRoute.\n");
  1853.     return FAILURE;
  1854. d947 1
  1855. a947 2
  1856.     status = Net_InstallRoute(sizeof(Net_RouteInfo), &routeInfo);
  1857.     return status;
  1858. d949 1
  1859. d954 1
  1860. a954 1
  1861.  * DownloadFile --
  1862. d956 2
  1863. a957 1
  1864.  *    Installs all routes from the host database
  1865. d960 3
  1866. a962 1
  1867.  *    SUCCESS if all routes were installed, FAILURE otherwise
  1868. d969 3
  1869. a971 3
  1870.  
  1871. ReturnStatus
  1872. DownloadFile()
  1873. d973 4
  1874. d978 27
  1875. a1004 6
  1876.     int            spriteID;
  1877.     ReturnStatus    status;
  1878.     Host_Entry        *hostPtr;
  1879.  
  1880.     if (verbose) {
  1881.     printf("netroute: Downloading routes from host database.\n");
  1882. d1006 10
  1883. a1015 35
  1884.     /*
  1885.      * First we try an internet route to ourselves.  This allows the
  1886.      * kernel to know its internet address, which is needed to
  1887.      * install internet routes to other machines.  If this step
  1888.      * is skipped then the kernel will reverse arp for its address,
  1889.      * but that can't always be guaranteed to work.
  1890.      */
  1891.     Host_Start();
  1892.     status = Proc_GetHostIDs(NULL, &spriteID);
  1893.     if (status != SUCCESS) {
  1894.     printf("Couldn't get sprite ID of the current host.\n");
  1895.     } else {
  1896.     hostPtr = Host_ByID(spriteID);
  1897.     if (hostPtr != NULL) {
  1898.         hostPtr->netType = HOST_INET;
  1899.         if (ultraAddressPtr != NULL) {
  1900.         status = InstallRoute(hostPtr, ultraAddressPtr, 
  1901.                       NET_NETWORK_ULTRA);
  1902.         } else if (fddiAddressPtr != NULL) {
  1903.         status = InstallRoute(hostPtr, fddiAddressPtr, 
  1904.                       NET_NETWORK_FDDI);
  1905.         } else {
  1906.         status = InstallRoute(hostPtr, NULL, 
  1907.                       NET_NETWORK_ETHER);
  1908.         }
  1909.         if (status != SUCCESS) {
  1910.         printf("Couldn't install local inet route: %s\n", 
  1911.                Stat_GetMsg(status));
  1912.         }
  1913.     }
  1914.     }
  1915.     Host_Start();
  1916.     while ((hostPtr = Host_Next()) != (Host_Entry *)NULL) {
  1917.     if (!keepDomain) {
  1918.         FixHostName(hostPtr->name);
  1919. d1017 48
  1920. a1064 6
  1921.     if (ultraAddressPtr != NULL) {
  1922.         status = InstallRoute(hostPtr, ultraAddressPtr, NET_NETWORK_ULTRA);
  1923.     } else if (fddiAddressPtr != NULL) {
  1924.         status = InstallRoute(hostPtr, fddiAddressPtr, NET_NETWORK_FDDI);
  1925.     } else {
  1926.         status = InstallRoute(hostPtr, NULL, NET_NETWORK_ETHER);
  1927. d1066 14
  1928. a1079 3
  1929.     if (status != SUCCESS) {
  1930.         printf("Couldn't install route to host %s : %s\n", 
  1931.         hostPtr->name, Stat_GetMsg(status));
  1932. d1083 6
  1933. a1088 4
  1934.     printf("netroute: Closing host database\n");
  1935.     }
  1936.     Host_End();
  1937.     return status;
  1938. d1090 1
  1939. d1095 9
  1940. a1103 1
  1941.  * Daemon --
  1942. d1105 79
  1943. a1183 49
  1944.  *    This procedure is a daemon that will check the status of the
  1945.  *    host database every interval, and download the routes if
  1946.  *    the database has been modified.
  1947.  *
  1948.  * Results:
  1949.  *    None.
  1950.  *
  1951.  * Side effects:
  1952.  *    None.
  1953.  *
  1954.  *----------------------------------------------------------------------
  1955.  */
  1956.  
  1957. void
  1958. Daemon(interval, range)
  1959.     unsigned int    interval;
  1960.     unsigned int    range;
  1961. {
  1962.     struct stat        stats;
  1963.     time_t        modifyTime;
  1964.     Boolean        detach = TRUE;
  1965.     int            spriteID;
  1966.     struct timeval    bedtime, wakeup;
  1967.     int            snooze;
  1968.     FILE        *tmp;
  1969.  
  1970.     bzero((char *) &stats, sizeof(stats));
  1971.     bzero((char *) &modifyTime, sizeof(time_t));
  1972.     srandom(getpid());
  1973.     printf("Netroute daemon starting up.\n");
  1974.     if (output != NULL) {
  1975.     tmp = freopen(output, "w", stdout);
  1976.     if (tmp == NULL) {
  1977.         printf("netroute: can't open output file \"%s\"\n", output);
  1978.     }
  1979.     }
  1980.     while(1) {
  1981.     if (debug) {
  1982.         printf("Checking host database.\n");
  1983.     }
  1984.     if (Host_Stat(&stats) != 0) {
  1985. #if 0
  1986.         printf("%s: stat of host database file failed: %s\n", myname,
  1987.         sys_errlist[errno]);
  1988.         /*
  1989.          * Don't exit because stat() will fail if the server is down.
  1990.          * Assume that the server is down and keep on going.
  1991.          */
  1992.         printf("%s exiting\n", myname);
  1993. d1185 2
  1994. a1186 24
  1995. #endif
  1996.     } else if (modifyTime != stats.st_mtime) {
  1997.         if (verbose) {
  1998.         printf(
  1999.         "netroute: spritehosts modified, old time %d, new time %d\n",
  2000.             modifyTime, stats.st_mtime);
  2001.         }
  2002.         DownloadFile();
  2003.         modifyTime = stats.st_mtime;
  2004.     }
  2005.     if (detach) {
  2006.         Proc_Detach(0);
  2007.         detach = FALSE;
  2008.     }
  2009.     snooze = interval +  (random() % range);
  2010.     if (debug) {
  2011.         printf("Snoozing for %d seconds\n", snooze);
  2012.         gettimeofday(&bedtime, NULL);
  2013.     }
  2014.     sleep(snooze);
  2015.     if (debug) {
  2016.         gettimeofday(&wakeup, NULL);
  2017.         printf("snooze = %d\n", snooze);
  2018.         printf("slept = %d\n", wakeup.tv_sec - bedtime.tv_sec);
  2019. d1188 1
  2020. a1189 1
  2021. }
  2022. d1191 5
  2023. @
  2024.  
  2025.  
  2026. 1.16
  2027. log
  2028. @*** empty log message ***
  2029. @
  2030. text
  2031. @d19 1
  2032. a19 1
  2033. static char rcsid[] = "$Header: /sprite/src/cmds/netroute/RCS/netroute.c,v 1.15 92/02/14 19:10:50 jhh Exp Locker: jhh $ SPRITE (Berkeley)";
  2034. d53 1
  2035. d78 2
  2036. d94 1
  2037. a94 1
  2038.                 Net_UltraAddress *ultraAddressPtr));
  2039. d102 4
  2040. d107 1
  2041. d179 1
  2042. a179 1
  2043.         printf(stderr, "netroute: malformed ultranet address %s\n",
  2044. d184 11
  2045. d203 4
  2046. d208 1
  2047. a208 1
  2048.     fprintf("netroute: Bad net type '%s', must be 'ether' or 'inet'.\n", 
  2049. d251 3
  2050. d351 12
  2051. a362 1
  2052.        status = InstallRoute(host, ultraAddressPtr);
  2053. d364 3
  2054. a366 3
  2055.         printf("Couldn't install route to host %s : %s\n", 
  2056.             host->name, Stat_GetMsg(status));
  2057.         }
  2058. d422 1
  2059. a422 1
  2060. InstallRoute(hostPtr, ultraAddressPtr)
  2061. d424 2
  2062. a425 1
  2063.     Net_UltraAddress    *ultraAddressPtr;
  2064. d430 2
  2065. a440 1
  2066.     routeInfo.netType = NET_NETWORK_ETHER;
  2067. d445 5
  2068. a449 1
  2069.     if (ultraAddressPtr == NULL) {
  2070. d470 3
  2071. a472 1
  2072.     } else {
  2073. d476 10
  2074. d531 16
  2075. a546 5
  2076.     hostPtr->netType = HOST_INET;
  2077.     status = InstallRoute(hostPtr, ultraAddressPtr);
  2078.     if (status != SUCCESS) {
  2079.         printf("Couldn't install local inet route: %s\n", 
  2080.         Stat_GetMsg(status));
  2081. d554 7
  2082. a560 1
  2083.     status = InstallRoute(hostPtr, ultraAddressPtr);
  2084. @
  2085.  
  2086.  
  2087. 1.15
  2088. log
  2089. @*** empty log message ***
  2090. @
  2091. text
  2092. @d19 1
  2093. a19 1
  2094. static char rcsid[] = "$Header: /sprite/src/cmds/netroute.new/RCS/netroute.c,v 1.14 90/12/01 21:00:48 jhh Exp Locker: jhh $ SPRITE (Berkeley)";
  2095. @
  2096.  
  2097.  
  2098. 1.15.1.1
  2099. log
  2100. @Initial branch for Sprite server.
  2101. @
  2102. text
  2103. @d19 1
  2104. a19 1
  2105. static char rcsid[] = "$Header: /sprite/src/cmds/netroute/RCS/netroute.c,v 1.15 92/02/14 19:10:50 jhh Exp $ SPRITE (Berkeley)";
  2106. @
  2107.  
  2108.  
  2109. 1.14
  2110. log
  2111. @lots of changes,  made it a daemon
  2112. @
  2113. text
  2114. @d19 1
  2115. a19 1
  2116. static char rcsid[] = "$Header: /sprite/src/cmds/netroute.new/RCS/netroute.c,v 1.13 90/09/26 12:12:21 jhh Exp Locker: jhh $ SPRITE (Berkeley)";
  2117. d38 1
  2118. d54 2
  2119. a55 1
  2120. unsigned int interval = 120;
  2121. d57 2
  2122. d80 4
  2123. d257 1
  2124. a257 1
  2125.     Daemon(interval);
  2126. d460 2
  2127. a461 2
  2128.     if (debug) {
  2129.     printf("Downloading routes from host database.\n");
  2130. d494 3
  2131. d520 1
  2132. a520 1
  2133. Daemon(interval)
  2134. d522 1
  2135. a525 1
  2136.     int    fd;
  2137. d527 4
  2138. d534 8
  2139. d558 5
  2140. d570 11
  2141. a580 1
  2142.     sleep(interval);
  2143. @
  2144.  
  2145.  
  2146. 1.13
  2147. log
  2148. @new system call interface
  2149. @
  2150. text
  2151. @d19 1
  2152. a19 1
  2153. static char rcsid[] = "$Header: /a/newcmds/netroute/RCS/netroute.c,v 1.12 89/09/14 13:16:43 douglis Exp $ SPRITE (Berkeley)";
  2154. d23 15
  2155. a37 9
  2156. #include "sprite.h"
  2157. #include "fs.h"
  2158. #include "net.h"
  2159. #include "host.h"
  2160. #include "kernel/netRoute.h"
  2161. #include "stdio.h"
  2162. #include "option.h"
  2163. #include "rpc.h"
  2164. #include "sysStats.h"
  2165. a43 1
  2166. Boolean setInetAddress = FALSE;
  2167. d52 3
  2168. d60 13
  2169. a72 4
  2170.     {OPT_STRING, "h", (Address)&hostName, "Install route for Sprite host with given name or Sprite ID"},
  2171.     {OPT_STRING, "i", (Address)&inetString, "Internet address for route. (aa.bb.cc.dd)"},
  2172.     {OPT_STRING, "e", (Address)ðerString, "Ethernet address for host. (aa:bb:cc:dd:ee:ff)"},
  2173.     {OPT_STRING, "u", (Address) &ultraString, "Ultranet address for host. (a/b)"},
  2174. a75 1
  2175.     {OPT_TRUE, "s", (Address)&setInetAddress, "Set internet address of host"},
  2176. d81 12
  2177. d104 1
  2178. d111 2
  2179. a112 1
  2180.     Host_NetType    netType;
  2181. d116 1
  2182. d118 1
  2183. a118 2
  2184.     Net_UltraAddress    *ultraAddressPtr = NULL;
  2185.     static Net_Address    netAddress;
  2186. d120 1
  2187. d130 1
  2188. a130 2
  2189.     } else {
  2190.     Host_Start();
  2191. d191 1
  2192. d242 1
  2193. d248 20
  2194. a267 9
  2195.     if (setInetAddress) {
  2196.     int        junk;
  2197.     NetInetRoute     inetRoute;
  2198.     extern int errno;
  2199.     status = Proc_GetHostIDs(&junk, &hostID);
  2200.     if (status != SUCCESS) {
  2201.         errno = Compat_MapCode(status);
  2202.         perror("Proc_GetHostIDs");
  2203.         exit(1);
  2204. d269 5
  2205. a273 2
  2206.         if (inetAddress == 0) {
  2207.         host = Host_ByID(hostID);
  2208. d275 3
  2209. a277 4
  2210.         fprintf(stderr,
  2211.     "netroute: Can not set INET address: %d not in host database.\n", 
  2212.                hostID);
  2213.          exit(1);
  2214. d279 13
  2215. a291 5
  2216.         bcopy(&host->inetAddr, &inetRoute.inetAddr,
  2217.           sizeof(inetRoute.inetAddr));
  2218.     } else {
  2219.         bcopy(&inetAddress, &inetRoute.inetAddr,
  2220.           sizeof(inetRoute.inetAddr));
  2221. d293 19
  2222. a311 11
  2223.     routeInfo.spriteID = hostID;
  2224.     routeInfo.interface = 0;
  2225.     routeInfo.protocol = NET_PROTO_INET;
  2226.     routeInfo.netType = NET_NETWORK_ETHER;
  2227.     routeInfo.netAddress[NET_PROTO_RAW].ether = *etherAddressPtr;
  2228.     routeInfo.netAddress[NET_PROTO_INET].inet = inetRoute.inetAddr;
  2229.     strcpy(routeInfo.hostname, "Localhost");
  2230.     strcpy(routeInfo.machType, "UnknownMachType");
  2231.     status = Net_InstallRoute(sizeof(Net_RouteInfo), &routeInfo);
  2232.     if (status != SUCCESS) {
  2233.         exit(1);
  2234. d313 14
  2235. a326 4
  2236.     if (inputFile == (char *)0) {
  2237.         exit(1);
  2238.     }
  2239.     Host_Start();
  2240. d328 23
  2241. a350 15
  2242.     /*
  2243.      * If no host is specified then initialize the table from a file.
  2244.      */
  2245.     if (hostName == (char *)0) {
  2246.     while ((host = Host_Next()) != (Host_Entry *)NULL) {
  2247.         if (!keepDomain) {
  2248.         FixHostName(host->name);
  2249.         }
  2250.         routeInfo.spriteID = host->id;
  2251.         routeInfo.interface = 0;
  2252.         routeInfo.protocol = NET_PROTO_RAW;
  2253.         routeInfo.netType = NET_NETWORK_ETHER;
  2254.         strcpy(routeInfo.hostname, host->name);
  2255.         strcpy(routeInfo.machType, host->machType);
  2256.         if (host->netType == HOST_ETHER) {
  2257. d352 4
  2258. a355 27
  2259.         routeInfo.protocol = NET_PROTO_RAW;
  2260.         /*
  2261.          * This is a hack because the types don't match.
  2262.          */
  2263.         bcopy(&host->netAddr.etherAddr,
  2264.             &routeInfo.netAddress[NET_PROTO_RAW].ether, 6);
  2265.         status = Net_InstallRoute(sizeof(Net_RouteInfo), &routeInfo);
  2266.         } else if (host->netType == HOST_INET) {
  2267.         NetInetRoute     inetRoute;
  2268.         bcopy(&host->inetAddr, &inetRoute.inetAddr, 
  2269.               sizeof(inetRoute.inetAddr));
  2270.         bcopy(&host->netAddr.etherAddr, 
  2271.               &inetRoute.gatewayAddress, 
  2272.               sizeof(Net_EtherAddress));
  2273.         routeInfo.protocol = NET_PROTO_INET;
  2274.         routeInfo.netAddress[NET_PROTO_RAW].ether = 
  2275.             inetRoute.gatewayAddress;
  2276.         routeInfo.netAddress[NET_PROTO_INET].inet =
  2277.             inetRoute.inetAddr;
  2278.         status = Net_InstallRoute(sizeof(Net_RouteInfo), &routeInfo);
  2279.         } else {
  2280.         fprintf(stderr, "Unknown net type %d for %s\n",
  2281.             host->netType, host->name);
  2282.         }
  2283.         if (status != SUCCESS) {
  2284.         exit(1);
  2285.         }
  2286. a356 15
  2287.     exit(1);
  2288.     } 
  2289.     /*
  2290.      * Otherwise, Install a route for a single Sprite host. See if 
  2291.      * we can find the default values of the route from the Host 
  2292.      * database.  We know the user specified either a hostID or 
  2293.      * hostName so we lookup in the Host database until we find it.
  2294.      */
  2295.     /*
  2296.      * First see if it is a valid hostname if not a name then a number.
  2297.      */
  2298.     host = Host_ByName(hostName);
  2299.     if (host == (Host_Entry *)NULL) {
  2300.     hostID = atoi(hostName);
  2301.     host = Host_ByID(hostID);
  2302. d358 30
  2303. d389 2
  2304. a390 2
  2305.      * Fill in hostID, hostName, and etherAddress letting the user
  2306.      * specified values override the values in the data base.
  2307. a391 50
  2308.     if (hostID < 0) {
  2309.     if (host == (Host_Entry *)NULL) {
  2310.         fprintf(stderr, "netroute: Can not compute spriteID for %s\n",
  2311.              hostName);
  2312.         exit(1);
  2313.     }
  2314.     hostID = host->id;
  2315.    }
  2316.    if (hostName == (char *) 0) {
  2317.        if (host == (Host_Entry *)NULL) {
  2318.         static char nameBuffer[64];
  2319.         sprintf(nameBuffer, "host%d", hostID);
  2320.         hostName = nameBuffer;
  2321.        } else {
  2322.         hostName = host->name;
  2323.        }
  2324.    }
  2325.    if (etherAddressPtr == (Net_EtherAddress *) 0) {
  2326.        if (host == (Host_Entry *)NULL) {
  2327.         fprintf(stderr, "netroute: Need -e option.\n");
  2328.         exit(1);
  2329.        } else {
  2330.        etherAddressPtr = (Net_EtherAddress *) (host->netAddr.etherAddr);
  2331.        }
  2332.    }
  2333.    if (machType == (char *) 0) {
  2334.        if (host == (Host_Entry *)NULL) {
  2335.         fprintf(stderr, "netroute: Need -m option.\n");
  2336.         exit(1);
  2337.        } else {
  2338.        machType = host->machType;
  2339.        }
  2340.    }
  2341.    /*
  2342.     * INET routes need the internet address.
  2343.     */
  2344.    if (netType == HOST_INET) {
  2345.        if (inetAddress == 0) {
  2346.        if (host == (Host_Entry *)NULL) {
  2347.         fprintf(stderr, "netroute: Need -i option.\n");
  2348.         exit(1);
  2349.        }
  2350.        bcopy(&(host->inetAddr), &inetAddress, sizeof(host->inetAddr));
  2351.        }
  2352.    } 
  2353.  
  2354.    if (!keepDomain) {
  2355.     FixHostName(hostName);
  2356.    }
  2357.     routeInfo.spriteID = hostID;
  2358. d393 7
  2359. a399 7
  2360.     strcpy(routeInfo.hostname, hostName);
  2361.     strcpy(routeInfo.machType, machType);
  2362.     if (ultraString == NULL) {
  2363.     routeInfo.netType = NET_NETWORK_ETHER;
  2364.     NET_ETHER_ADDR_COPY(*etherAddressPtr, 
  2365.         routeInfo.netAddress[NET_PROTO_RAW].ether);
  2366.        if (netType == HOST_ETHER) {
  2367. d401 6
  2368. a406 5
  2369.         status = Net_InstallRoute(sizeof(Net_RouteInfo), &routeInfo);
  2370.        } else { /* netType == HOST_INET */
  2371.         NetInetRoute     inetRoute;
  2372.         inetRoute.inetAddr = inetAddress;
  2373.         NET_ETHER_ADDR_COPY(*etherAddressPtr,inetRoute.gatewayAddress);
  2374. d409 4
  2375. d414 5
  2376. a418 2
  2377.         status = Net_InstallRoute(sizeof(Net_RouteInfo), &routeInfo);
  2378.        }
  2379. a422 1
  2380.         status = Net_InstallRoute(sizeof(Net_RouteInfo), &routeInfo);
  2381. d424 2
  2382. a425 4
  2383.    if (status != SUCCESS) {
  2384.        exit(1);
  2385.    }
  2386.    exit (0);
  2387. d431 63
  2388. a493 1
  2389.  * FixHostName --
  2390. d495 3
  2391. a497 1
  2392.  *    Remove the domain part from a host name.
  2393. d503 1
  2394. a503 1
  2395.  *    String is modified in place.
  2396. d508 3
  2397. a510 3
  2398.  
  2399. FixHostName(charPtr)
  2400.      register char *charPtr;
  2401. d512 4
  2402. d517 24
  2403. a540 4
  2404.     for (; *charPtr != '\0' ; charPtr++) {
  2405.     if (*charPtr == '.') {
  2406.         *charPtr = '\0';
  2407.         break;
  2408. d542 1
  2409. @
  2410.  
  2411.  
  2412. 1.12
  2413. log
  2414. @check for bad status from Hos_SetFile
  2415. @
  2416. text
  2417. @d19 1
  2418. a19 1
  2419. static char rcsid[] = "$Header: /a/newcmds/netroute/RCS/netroute.c,v 1.11 89/07/07 14:46:58 mendel Exp Locker: douglis $ SPRITE (Berkeley)";
  2420. d46 1
  2421. d55 1
  2422. d86 3
  2423. a89 1
  2424.  
  2425. d102 2
  2426. d128 10
  2427. a137 1
  2428.  
  2429. d142 4
  2430. d155 1
  2431. a155 1
  2432.     Net_SpriteRoute route;
  2433. d157 2
  2434. d162 1
  2435. d167 6
  2436. d177 26
  2437. a202 20
  2438.         switch(route.type) {
  2439.         case NET_ROUTE_ETHER: {
  2440.             printf("%5d %-20s", spriteID, (host != NULL ? host->name :
  2441.                              (spriteID == 0 ?
  2442.                               "BROADCAST" : "???")));
  2443.             printf("%-8s", (host != NULL ? host->machType :
  2444.                         (spriteID == 0 ? "" : "???")));
  2445.             printf("ether %02x:%02x:%02x:%02x:%02x:%02x\n",
  2446.                 NET_ETHER_ADDR_BYTE1( NET_ETHER_HDR_DESTINATION(
  2447.                 route.route.etherHdr)) & 0xff,
  2448.                 NET_ETHER_ADDR_BYTE2( NET_ETHER_HDR_DESTINATION(
  2449.                 route.route.etherHdr)) & 0xff,
  2450.                 NET_ETHER_ADDR_BYTE3( NET_ETHER_HDR_DESTINATION(
  2451.                 route.route.etherHdr)) & 0xff,
  2452.                 NET_ETHER_ADDR_BYTE4( NET_ETHER_HDR_DESTINATION(
  2453.                 route.route.etherHdr)) & 0xff,
  2454.                 NET_ETHER_ADDR_BYTE5( NET_ETHER_HDR_DESTINATION(
  2455.                 route.route.etherHdr)) & 0xff,
  2456.                 NET_ETHER_ADDR_BYTE6( NET_ETHER_HDR_DESTINATION(
  2457.                 route.route.etherHdr)) & 0xff);
  2458. d204 2
  2459. a205 33
  2460.         }
  2461.         case NET_ROUTE_INET: {
  2462.             Net_EtherHdr *etherHdrPtr;
  2463.             Net_IPHeader ipHeader;
  2464.             unsigned int dest;
  2465.             char    inetBuffer[64];
  2466.  
  2467.             /*
  2468.              * Copy out the ipHeader because it is not word aligned.
  2469.              */
  2470.             bcopy(((char *)&route.route)+sizeof(Net_EtherHdr), 
  2471.               (char *) &ipHeader, sizeof(ipHeader));
  2472.             printf("%5d %-20s", spriteID, (host != NULL ? host->name :
  2473.                              (spriteID == 0 ?
  2474.                               "BROADCAST" : "???")));
  2475.             printf("%-8s", (host != NULL ? host->machType :
  2476.                         (spriteID == 0 ? "" : "???")));
  2477.             Net_InetAddrToString(Net_NetToHostInt(ipHeader.dest), 
  2478.                     inetBuffer);
  2479.             printf("inet %s gateway %02x:%02x:%02x:%02x:%02x:%02x\n",
  2480.                 inetBuffer,
  2481.                 NET_ETHER_ADDR_BYTE1( NET_ETHER_HDR_DESTINATION(
  2482.                 route.route.etherHdr)) & 0xff,
  2483.                 NET_ETHER_ADDR_BYTE2( NET_ETHER_HDR_DESTINATION(
  2484.                 route.route.etherHdr)) & 0xff,
  2485.                 NET_ETHER_ADDR_BYTE3( NET_ETHER_HDR_DESTINATION(
  2486.                 route.route.etherHdr)) & 0xff,
  2487.                 NET_ETHER_ADDR_BYTE4( NET_ETHER_HDR_DESTINATION(
  2488.                 route.route.etherHdr)) & 0xff,
  2489.                 NET_ETHER_ADDR_BYTE5( NET_ETHER_HDR_DESTINATION(
  2490.                 route.route.etherHdr)) & 0xff,
  2491.                 NET_ETHER_ADDR_BYTE6( NET_ETHER_HDR_DESTINATION(
  2492.                 route.route.etherHdr)) & 0xff);
  2493. a206 1
  2494.         }
  2495. d208 2
  2496. d211 4
  2497. a214 1
  2498.     exit(status);
  2499. d240 12
  2500. a251 2
  2501.     status = Net_InstallRoute(hostID, 0, NET_ROUTE_INET, &inetRoute,
  2502.                 "Localhost", "UnknownMachType");
  2503. d253 1
  2504. a253 1
  2505.         exit(status);
  2506. d265 6
  2507. d272 8
  2508. a279 3
  2509.         Net_InstallRoute(host->id, 0, NET_ROUTE_ETHER,
  2510.                 &host->netAddr.etherAddr,
  2511.                 host->name, host->machType);
  2512. d287 6
  2513. a292 2
  2514.         Net_InstallRoute(host->id, 0, NET_ROUTE_INET, &inetRoute,
  2515.                 host->name, host->machType);
  2516. d297 3
  2517. d301 1
  2518. a301 1
  2519.     exit(status);
  2520. d370 28
  2521. a397 9
  2522.    if (netType == HOST_ETHER) {
  2523.        status = Net_InstallRoute(hostID, 0, NET_ROUTE_ETHER,
  2524.                 etherAddressPtr,  hostName, machType);
  2525.    } else { /* netType == HOST_INET */
  2526.     NetInetRoute     inetRoute;
  2527.     inetRoute.inetAddr = inetAddress;
  2528.     NET_ETHER_ADDR_COPY(*etherAddressPtr,inetRoute.gatewayAddress);
  2529.         status = Net_InstallRoute(hostID, 0, NET_ROUTE_INET, &inetRoute,
  2530.                 hostName, machType);
  2531. d399 1
  2532. a399 1
  2533.    exit (status);
  2534. @
  2535.  
  2536.  
  2537. 1.11
  2538. log
  2539. @Rewind host file after each use.
  2540. @
  2541. text
  2542. @d19 1
  2543. a19 1
  2544. static char rcsid[] = "$Header: /a/newcmds/netroute/RCS/netroute.c,v 1.10 89/06/29 10:26:03 mendel Exp $ SPRITE (Berkeley)";
  2545. d83 1
  2546. d91 4
  2547. a94 1
  2548.     Host_SetFile(inputFile);
  2549. @
  2550.  
  2551.  
  2552. 1.10
  2553. log
  2554. @Added set inetaddress code.
  2555. @
  2556. text
  2557. @d19 1
  2558. a19 1
  2559. static char rcsid[] = "$Header: /a/newcmds/netroute/RCS/netroute.c,v 1.9 89/06/08 11:55:25 mendel Exp Locker: mendel $ SPRITE (Berkeley)";
  2560. d234 1
  2561. @
  2562.  
  2563.  
  2564. 1.9
  2565. log
  2566. @Rewrote to support inet routes.
  2567. @
  2568. text
  2569. @d19 1
  2570. a19 1
  2571. static char rcsid[] = "$Header: /a/newcmds/netroute/RCS/netroute.c,v 1.8 89/04/23 16:25:47 jhh Exp Locker: mendel $ SPRITE (Berkeley)";
  2572. d38 1
  2573. d57 1
  2574. d169 2
  2575. a170 1
  2576.             Net_IPHeader *ipHeaderPtr;
  2577. d173 5
  2578. a177 2
  2579.             ipHeaderPtr = (Net_IPHeader *) 
  2580.                 (((char *)&route.route)+sizeof(Net_EtherHdr));
  2581. d183 1
  2582. a183 2
  2583.  
  2584.             Net_InetAddrToString(Net_NetToHostInt(ipHeaderPtr->dest), 
  2585. d205 30
  2586. @
  2587.  
  2588.  
  2589. 1.8
  2590. log
  2591. @use macros in netEther.h to access fields in ether header
  2592. @
  2593. text
  2594. @d8 8
  2595. a15 2
  2596.  * Copyright 1987 Regents of the University of California
  2597.  * All rights reserved.
  2598. d19 1
  2599. a19 1
  2600. static char rcsid[] = "$Header: /a/newcmds/netroute/RCS/netroute.c,v 1.7 89/04/23 16:21:39 brent Exp Locker: jhh $ SPRITE (Berkeley)";
  2601. a37 1
  2602. Boolean gateway = FALSE;
  2603. d44 1
  2604. d47 2
  2605. a48 1
  2606.     {OPT_TRUE, "t", (Address)&table, "Print out the route table"},
  2607. d50 8
  2608. a57 9
  2609.     {OPT_INT, "h", (Address)&hostID, "Install route for Sprite host with given ID"},
  2610.     {OPT_STRING, "m", (Address)&machType, "Machine type (sun2, spur, etc.) needed with -h"},
  2611.     {OPT_STRING, "n", (Address)&hostName, "Install route for Sprite host with given name"},
  2612.     {OPT_STRING, "i", (Address)&inetString, "Install route for host at the given Internet Address (aa.bb.cc.dd)"},
  2613.     {OPT_STRING, "e", (Address)ðerString, "Ethernet Address for host specified with -h, -n or -i (aa:bb:cc:dd:ee:ff)"},
  2614.     {OPT_TRUE, "G", (Address)&gateway, "Use these routes as gateways"},
  2615.     {OPT_STRING, "f", (Address)&inputFile, "Specifies the host file to read info from."},
  2616.     {OPT_DOC, "\0", (Address)NULL, "If none of -h, -n or -i is specified, routes are installed for all hosts in"},
  2617.     {OPT_DOC, "\0", (Address)NULL, "the host database (or the input file if -f is given)"},
  2618. d78 3
  2619. d84 3
  2620. a86 1
  2621.  
  2622. d92 34
  2623. d140 2
  2624. a141 2
  2625.         if (!keepDomain) {
  2626.         FixHostName(host);
  2627. d144 1
  2628. a144 1
  2629.         case NET_ROUTE_ETHER:
  2630. a163 21
  2631.         }
  2632.     }
  2633.     } else {
  2634.     if ((hostID < 0) &&
  2635.         (hostName == (char *)0) &&
  2636.         (inetString == (char *)0)) {
  2637.         /*
  2638.          *  Initialize the table from a file.
  2639.          */
  2640.         while ((host = Host_Next()) != (Host_Entry *)NULL) {
  2641.             if (!keepDomain) {
  2642.             FixHostName(host);
  2643.             }
  2644.             if (host->netType == HOST_ETHER) {
  2645.             Net_InstallRoute(host->id, 0, NET_ROUTE_ETHER,
  2646.                     &host->netAddr.etherAddr,
  2647.                     host->name, host->machType);
  2648.             } else {
  2649.             fprintf(stderr, "Unknown net type for %s\n",
  2650.                     host->name);
  2651.             }
  2652. d165 12
  2653. a176 25
  2654.     } else if (etherString != (char *)0) {
  2655.         /*
  2656.          * Install a route for a particular ethernet address.
  2657.          */
  2658.          
  2659.         if (hostID > 0) {
  2660.         char name[50];
  2661.         sprintf(name, "host%d", hostID);
  2662.         if (machType == (char *)0) {
  2663.             fprintf(stderr, "Need -m <machType> with -h <ID>\n");
  2664.             status = FAILURE;
  2665.         } else {
  2666.             status = DoEtherAddress(hostID, etherString, name,machType);
  2667.         }
  2668.         } else if (hostName != (char *)0) {
  2669.         host = Host_ByName(hostName);
  2670.         if (host != (Host_Entry *)NULL) {
  2671.             status = DoEtherAddress(host->id, etherString, host->name,
  2672.                     host->machType);
  2673.         } else {
  2674.             fprintf(stderr, "Unknown host %s\n", hostName);
  2675.             status = FAILURE;
  2676.         }
  2677.         } else if (inetString != (char *)0) {
  2678.         Net_InetAddress    inetAddr;
  2679. d178 17
  2680. a194 9
  2681.         inetAddr = Net_StringToInetAddr(inetString);
  2682.         host = Host_ByInetAddr(inetAddr);
  2683.         if (host != (Host_Entry *)NULL) {
  2684.             status = DoEtherAddress(host->id, etherString, host->name,
  2685.                     host->machType);
  2686.         } else {
  2687.             fprintf(stderr, "No Sprite host at %s\n",
  2688.                     inetString);
  2689.             status = FAILURE;
  2690. d197 28
  2691. a224 2
  2692.     } else {
  2693.         Opt_PrintUsage(argv[0], numOptions, optionArray);
  2694. d226 15
  2695. d242 64
  2696. a305 48
  2697.         
  2698.     exit(status);
  2699. }
  2700.  
  2701. /*
  2702.  *----------------------------------------------------------------------
  2703.  *
  2704.  * DoEtherAddress --
  2705.  *
  2706.  *    Convert a string-format ethernet address to a structure
  2707.  *    and then call Net_InstallRoute to install the address
  2708.  *    into the kernel's routing table.
  2709.  *
  2710.  * Results:
  2711.  *    None.
  2712.  *
  2713.  * Side effects:
  2714.  *    None.
  2715.  *
  2716.  *----------------------------------------------------------------------
  2717.  */
  2718.  
  2719. ReturnStatus
  2720. DoEtherAddress(hostID, etherString, name, machType)
  2721.     int hostID;
  2722.     char *etherString;
  2723.     char *name;
  2724.     char *machType;
  2725. {
  2726.     int byte1, byte2, byte3, byte4, byte5, byte6;
  2727.     int numBytes;
  2728.     Net_EtherAddress etherAddr;
  2729.  
  2730.     numBytes = sscanf(etherString, "%x:%x:%x:%x:%x:%x",
  2731.             &byte1, &byte2, &byte3, &byte4, &byte5, &byte6);
  2732.     if (numBytes != 6) {
  2733.     fprintf(stderr, "Garbled ethernet address \"%s\"\n",
  2734.                   etherString);
  2735.     return(FAILURE);
  2736.     }
  2737.     NET_ETHER_ADDR_BYTE1(etherAddr) = byte1 & 0xff;
  2738.     NET_ETHER_ADDR_BYTE2(etherAddr) = byte2 & 0xff;
  2739.     NET_ETHER_ADDR_BYTE3(etherAddr) = byte3 & 0xff;
  2740.     NET_ETHER_ADDR_BYTE4(etherAddr) = byte4 & 0xff;
  2741.     NET_ETHER_ADDR_BYTE5(etherAddr) = byte5 & 0xff;
  2742.     NET_ETHER_ADDR_BYTE6(etherAddr) = byte6 & 0xff;
  2743.     return(Net_InstallRoute(hostID, 0, NET_ROUTE_ETHER, ðerAddr,
  2744.                 name, machType));
  2745. d311 1
  2746. a311 1
  2747.  * DoInetAddress --
  2748. d313 1
  2749. a313 3
  2750.  *    Convert a string-format inet address to a structure
  2751.  *    and then call Net_InstallRoute to install the address
  2752.  *    into the kernel's routing table.
  2753. d319 1
  2754. a319 1
  2755.  *    None.
  2756. a323 29
  2757. ReturnStatus
  2758. DoInetAddress(hostID, inetString, name, machType)
  2759.     int hostID;
  2760.     char *inetString;
  2761.     char *name;
  2762.     char *machType;
  2763. {
  2764.     int byte1, byte2, byte3, byte4;
  2765.     int numBytes;
  2766.     Net_InetAddress etherAddr;
  2767.  
  2768.     numBytes = sscanf(etherString, "%x.%x.%x.%x",
  2769.             &byte1, &byte2, &byte3, &byte4);
  2770.     if (numBytes != 4) {
  2771.     fprintf(stderr, "Garbled inet address \"%s\"\n",
  2772.                   etherString);
  2773.     return(FAILURE);
  2774.     }
  2775. #ifdef notdef
  2776.     etherAddr.byte1 = byte1 & 0xff;
  2777.     etherAddr.byte2 = byte2 & 0xff;
  2778.     etherAddr.byte3 = byte3 & 0xff;
  2779.     etherAddr.byte4 = byte4 & 0xff;
  2780.     etherAddr.byte5 = byte5 & 0xff;
  2781.     etherAddr.byte6 = byte6 & 0xff;
  2782. #endif 
  2783.     return(Net_InstallRoute(hostID, 0, NET_ROUTE_ETHER, ðerAddr,
  2784.                 name, machType));
  2785. }
  2786. d325 2
  2787. a326 2
  2788. FixHostName(host)
  2789.     Host_Entry *host;
  2790. a327 1
  2791.     register char *charPtr;
  2792. d329 4
  2793. a332 6
  2794.     if (host != (Host_Entry *)0) {
  2795.     for (charPtr = host->name; *charPtr != '\0' ; charPtr++) {
  2796.         if (*charPtr == '.') {
  2797.         *charPtr = '\0';
  2798.         break;
  2799.         }
  2800. @
  2801.  
  2802.  
  2803. 1.7
  2804. log
  2805. @Added proper macro use
  2806. @
  2807. text
  2808. @d13 1
  2809. a13 1
  2810. static char rcsid[] = "$Header: /a/newcmds/netroute/RCS/netroute.c,v 1.6 88/12/22 13:12:04 brent Exp $ SPRITE (Berkeley)";
  2811. d106 12
  2812. a117 12
  2813.                 NET_ETHER_ADDR_BYTE1(
  2814.                 route.route.etherHdr.destination) & 0xff,
  2815.                 NET_ETHER_ADDR_BYTE2(
  2816.                 route.route.etherHdr.destination) & 0xff,
  2817.                 NET_ETHER_ADDR_BYTE3(
  2818.                 route.route.etherHdr.destination) & 0xff,
  2819.                 NET_ETHER_ADDR_BYTE4(
  2820.                 route.route.etherHdr.destination) & 0xff,
  2821.                 NET_ETHER_ADDR_BYTE5(
  2822.                 route.route.etherHdr.destination) & 0xff,
  2823.                 NET_ETHER_ADDR_BYTE6(
  2824.                 route.route.etherHdr.destination) & 0xff);
  2825. @
  2826.  
  2827.  
  2828. 1.6
  2829. log
  2830. @Ported to standard C library
  2831. @
  2832. text
  2833. @d13 1
  2834. a13 1
  2835. static char rcsid[] = "$Header: /a/newcmds/netroute/RCS/netroute.c,v 1.5 88/11/12 12:06:18 brent Exp Locker: brent $ SPRITE (Berkeley)";
  2836. d32 1
  2837. d48 1
  2838. a80 5
  2839.     /*
  2840.      * Do one of three things, print the route table,
  2841.      * initialize the route table from a file,
  2842.      * or add a route using the command line arguments.
  2843.      */
  2844. d82 3
  2845. d106 12
  2846. a117 6
  2847.                 route.route.etherHdr.destination.byte1 & 0xff,
  2848.                 route.route.etherHdr.destination.byte2 & 0xff,
  2849.                 route.route.etherHdr.destination.byte3 & 0xff,
  2850.                 route.route.etherHdr.destination.byte4 & 0xff,
  2851.                 route.route.etherHdr.destination.byte5 & 0xff,
  2852.                 route.route.etherHdr.destination.byte6 & 0xff);
  2853. d125 3
  2854. d142 4
  2855. d185 18
  2856. d222 47
  2857. d275 1
  2858. @
  2859.  
  2860.  
  2861. 1.5
  2862. log
  2863. @Added machtype to Net_InstallRoute calls
  2864. @
  2865. text
  2866. @d2 1
  2867. a2 1
  2868.  * netRoute.c --
  2869. d4 3
  2870. a6 1
  2871.  *    User program to install routes to Sprite Hosts.
  2872. d13 1
  2873. a13 1
  2874. static char rcsid[] = "$Header: netRoute.c,v 1.4 88/03/31 10:06:42 brent Exp $ SPRITE (Berkeley)";
  2875. d22 1
  2876. a22 1
  2877. #include "io.h"
  2878. d40 10
  2879. a49 10
  2880.     {OPT_TRUE, 't', (Address)&table, "Print out the route table"},
  2881.     {OPT_TRUE, 'D', (Address)&keepDomain, "Keep domain suffix on host name"},
  2882.     {OPT_INT, 'h', (Address)&hostID, "Install route for Sprite host with given ID"},
  2883.     {OPT_STRING, 'm', (Address)&machType, "Machine type (sun2, spur, etc.) needed with -h"},
  2884.     {OPT_STRING, 'n', (Address)&hostName, "Install route for Sprite host with given name"},
  2885.     {OPT_STRING, 'i', (Address)&inetString, "Install route for host at the given Internet Address (aa.bb.cc.dd)"},
  2886.     {OPT_STRING, 'e', (Address)ðerString, "Ethernet Address for host specified with -h, -n or -i (aa:bb:cc:dd:ee:ff)"},
  2887.     {OPT_STRING, 'f', (Address)&inputFile, "Specifies the host file to read info from."},
  2888.     {OPT_DOC, '\0', (Address)NULL, "If none of -h, -n or -i is specified, routes are installed for all hosts in"},
  2889.     {OPT_DOC, '\0', (Address)NULL, "the host database (or the input file if -f is given)"},
  2890. d72 1
  2891. a72 1
  2892.     Opt_Parse(&argc, argv, numOptions, optionArray);
  2893. d88 1
  2894. a88 1
  2895.     Io_Print("Sprite Route Table:\n");
  2896. d100 1
  2897. a100 1
  2898.             Io_Print("%5d %-20s", spriteID, (host != NULL ? host->name :
  2899. d103 1
  2900. a103 1
  2901.             Io_Print("%-8s", (host != NULL ? host->machType :
  2902. d105 1
  2903. a105 1
  2904.             Io_Print("ether %02x:%02x:%02x:%02x:%02x:%02x\n",
  2905. d128 1
  2906. a128 1
  2907.             Io_PrintStream(io_StdErr, "Unknown net type for %s\n",
  2908. d135 1
  2909. a135 1
  2910.         Io_PrintString(name, "%d", hostID);
  2911. d137 1
  2912. a137 2
  2913.             Io_PrintStream(io_StdErr,
  2914.             "Need -m <machType> with -h <ID>\n");
  2915. d140 1
  2916. a140 2
  2917.             status = DoEtherAddress(hostID, etherString, name,
  2918.                         machType);
  2919. d148 1
  2920. a148 1
  2921.             Io_PrintStream(io_StdErr, "Unknown host %s\n", hostName);
  2922. d160 1
  2923. a160 1
  2924.             Io_PrintStream(io_StdErr, "No Sprite host at %s\n",
  2925. d170 1
  2926. a170 1
  2927.     Proc_Exit(status);
  2928. d184 1
  2929. a184 1
  2930.     numBytes = Io_ScanString(etherString, "%x:%x:%x:%x:%x:%x",
  2931. d187 1
  2932. a187 1
  2933.     Io_PrintString(io_StdErr, "Garbled ethernet address \"%s\"\n",
  2934. @
  2935.  
  2936.  
  2937. 1.4
  2938. log
  2939. @Changed Net_InstallRoute interface to include hostname
  2940. @
  2941. text
  2942. @d11 1
  2943. a11 1
  2944. static char rcsid[] = "$Header: netRoute.c,v 1.3 87/10/06 13:01:46 deboor Exp $ SPRITE (Berkeley)";
  2945. d35 1
  2946. d41 1
  2947. d101 2
  2948. d124 1
  2949. a124 1
  2950.                     host->name);
  2951. d134 8
  2952. a141 1
  2953.         status = DoEtherAddress(hostID, etherString, name);
  2954. d145 2
  2955. a146 1
  2956.             status = DoEtherAddress(host->id, etherString, host->name);
  2957. d157 2
  2958. a158 1
  2959.             status = DoEtherAddress(host->id, etherString, host->name);
  2960. d174 1
  2961. a174 1
  2962. DoEtherAddress(hostID, etherString, name)
  2963. d178 1
  2964. d197 2
  2965. a198 1
  2966.     return(Net_InstallRoute(hostID, 0, NET_ROUTE_ETHER, ðerAddr, name));
  2967. @
  2968.  
  2969.  
  2970. 1.3
  2971. log
  2972. @Adapted to new hostfile calls
  2973. @
  2974. text
  2975. @d11 1
  2976. a11 1
  2977. static char rcsid[] = "$Header: netRoute.c,v 1.2 87/09/30 21:52:46 deboor Exp $ SPRITE (Berkeley)";
  2978. d29 1
  2979. d38 1
  2980. d91 3
  2981. d114 3
  2982. d119 2
  2983. a120 1
  2984.                     &host->netAddr.etherAddr);
  2985. d128 3
  2986. a130 1
  2987.         status = DoEtherAddress(hostID, etherString);
  2988. d134 1
  2989. a134 1
  2990.             status = DoEtherAddress(host->id, etherString);
  2991. d145 1
  2992. a145 1
  2993.             status = DoEtherAddress(host->id, etherString);
  2994. d161 1
  2995. a161 1
  2996. DoEtherAddress(hostID, etherString)
  2997. d164 1
  2998. d183 1
  2999. a183 1
  3000.     return(Net_InstallRoute(hostID, 0, NET_ROUTE_ETHER, ðerAddr));
  3001. d185 16
  3002. @
  3003.  
  3004.  
  3005. 1.2
  3006. log
  3007. @Adapted to new Host functions
  3008. @
  3009. text
  3010. @d11 1
  3011. a11 1
  3012. static char rcsid[] = "$Header: netRoute.c,v 1.1 87/08/05 22:20:53 andrew Exp $ SPRITE (Berkeley)";
  3013. d68 5
  3014. a104 9
  3015.     if (inputFile != (char *)0) {
  3016.         if (Host_SetFile(inputFile) != SUCCESS) {
  3017.         Io_PrintStream(io_StdErr, "Can't open \"%s\"\n", inputFile);
  3018.         Proc_Exit(1);
  3019.         }
  3020.     } else {
  3021.         Host_Start();
  3022.     }
  3023.  
  3024. @
  3025.  
  3026.  
  3027. 1.1
  3028. log
  3029. @Initial revision
  3030. @
  3031. text
  3032. @d11 1
  3033. a11 1
  3034. static char rcsid[] = "$Header: netTest.c,v 1.1 87/02/05 18:38:25 andrew Exp $ SPRITE (Berkeley)";
  3035. d18 1
  3036. d37 7
  3037. a43 5
  3038.     {OPT_INT, 'h', (Address)&hostID, "Sprite Host ID"},
  3039.     {OPT_STRING, 'n', (Address)&hostName, "Sprite Host Name"},
  3040.     {OPT_STRING, 'e', (Address)ðerString, "Ethernet Address, aa:bb:cc:dd:ee:ff"},
  3041.     {OPT_STRING, 'i', (Address)&inetString, "Internet Address, aa.bb.cc.dd"},
  3042.     {OPT_STRING, 'f', (Address)&inputFile, "Input filename"},
  3043. d45 1
  3044. a45 1
  3045. int numOptions = sizeof(optionArray) / sizeof(Option);
  3046. d63 1
  3047. d65 1
  3048. d83 1
  3049. d86 3
  3050. a88 1
  3051.             Io_Print("%5d ", spriteID);
  3052. d99 6
  3053. a104 7
  3054.     } else if (inputFile != (char *)0) {
  3055.     Io_Stream stream;
  3056.     char buffer[128];
  3057.  
  3058.     stream = Io_Open(inputFile, "r");
  3059.     if (stream == (Io_Stream)NULL) {
  3060.         Io_PrintStream(io_StdErr, "Can't open \"%s\"\n", inputFile);
  3061. d106 2
  3062. a107 6
  3063.         while (Io_GetLine(stream, 128, buffer) != NULL) {
  3064.         int numFields;
  3065.         int hostID;
  3066.         char typeName[40];
  3067.         char addressName[40];
  3068.         char name[40];
  3069. d109 11
  3070. a119 7
  3071.         numFields = Io_ScanString(buffer, "%d %40s %40s %40s", &hostID,
  3072.                           typeName, addressName, name);
  3073.         if (numFields != 4) {
  3074.             Io_PrintStream(io_StdErr, 
  3075.                 "Garbled input line \"%s\"\n", buffer);
  3076.         } else if (String_Compare(typeName, "ethernet") == 0) {
  3077.             status = DoEtherAddress(hostID, addressName);
  3078. d121 23
  3079. d145 1
  3080. a145 4
  3081.     }
  3082.     } else {
  3083.     if (hostID < 0) {
  3084.         Io_PrintStream(io_StdErr, "Need a host ID\n");
  3085. a146 1
  3086.         Proc_Exit(1);
  3087. a147 13
  3088.     if (hostName != (char *)0) {
  3089.         Io_PrintStream(io_StdErr, "Hostname argument ignored\n");
  3090.     }
  3091.     if (inetString != (char *)0) {
  3092.         Io_PrintStream(io_StdErr, "Internet Address ignored\n");
  3093.     }
  3094.     if (etherString == (char *)0) {
  3095.         Io_PrintStream(io_StdErr, "Need an ethernet address\n");
  3096.         Opt_PrintUsage(argv[0], numOptions, optionArray);
  3097.         Proc_Exit(1);
  3098.     } else {
  3099.         status = DoEtherAddress(hostID, etherString);
  3100.     }
  3101. d149 1
  3102. @
  3103.